如何使用jquery检测特殊字符

How to detect special characters using jquery

本文关键字:特殊字符 检测 jquery 何使用      更新时间:2024-04-19

我有一些html,看起来像这样:

<div id="toggle">
∅
</div>

我想使用jquery检测togglediv中的符号,然后更改该符号。我用&#8709; 制作了上面的符号

这是我尝试使用的jQuery,但没有发生任何事情,这让我相信符号没有被检测到。

$("#toggle").on("click", function () {
         if ($(this).html() == "&#8709;") {
              $(this).html("O");
           }
});

假设您的代码保存为UTF-8,则可以将该字符用作字符串

$("#toggle2").on("click", function () {
    if ($(this).html()=='∅') {
        $(this).html("O");
    }
});

或者你可以通过字符代码

$("#toggle").on("click", function () {
    if ($(this).html().charCodeAt(0)==8709) {
        $(this).html("O");
    }
});

还有额外的学分。。。替换所有发生的事件。

$("#toggle3").on("click", function () {
    $(this).html($(this).html().replace(/['u2205]/g,'o'));
});

http://jsfiddle.net/RmYM2/1/

.html()返回实际字符,因此需要执行

if ($(this).html() == "∅") {