简体   繁体   English

使用javascript将特殊字符转换回HTML实体代码

[英]Use javascript to convert special character back to HTML entity code

In one function of my site, the inner HTML of a div element is changed to include a right arrow. 在我网站的一项功能中,将div元素的内部HTML更改为包括向右箭头。 This symbol is included in the innerHTML of "myDiv" as ➞ 此符号作为➞包含在“ myDiv”的innerHTML中➞

In a separate function, I am trying to match the innerHTML of "myDiv" with ➞ 在一个单独的函数中,我试图将“ myDiv”的innerHTML与➞

function myFunction() {
//Many lines of code then
    var myText = document.getElementById("myDiv").innerHTML;
    alert(myText);
    if (myText.match(/➞/g)) { //also tried with /➞/g
         alert('Yep');
         myText = myText.replace(/➞/g, " to yield ");
    }
//Many more lines
}

When I alert myText as in the function above (before the if statement), the alerted output contains an actual right arrow ➞, not ➞ 当我按照上述函数(在if语句之前)警告myText时,警告输出包含实际的右箭头➞,而不是➞ The problem is that even though ➞ 问题是,即使➞ is present in the code to produce the innerHTML of "myDiv" in the first place (in a separate function), in myFunction() ➞ 首先出现在代码中,以在myFunction() ➞ (在单独的函数中)生成“ myDiv”的innerHTML ➞ does not seem to match, since the alert('Yep') is not called. 似乎不匹配,因为未调用alert('Yep')。

I looked up and in desperation tried decodeURIComponent() but I was pretty sure this was not going to work and it seemed not to. 我抬起头,无奈地尝试了encodeURIComponent(),但是我很确定这是行不通的,而且似乎也没有。 Can anyone help with this problem? 任何人都可以帮助解决这个问题吗? Thanks 谢谢

Check it against an actual right arrow, then. 然后,对照实际的右箭头进行检查。

function myFunction() {
//Many lines of code then
    var myText = document.getElementById("myDiv").innerHTML;
    alert(myText);
    if (myText.match(/➞/g)) { //also tried with /➞/g
         alert('Yep');
         myText = myText.replace(/➞/g, " to yield ");
    }
//Many more lines
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM