简体   繁体   English

如何使用jQuery检测特殊字符

[英]How to detect special characters using jquery

I have some html that looks like this: 我有一些看起来像这样的html:

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

I want to detect the symbol that is in the toggle div using jquery, then change the symbol. 我想使用jquery检测切换div中的符号,然后更改该符号。 I made the symbol above using &#8709; 我使用&#8709;

Here is the jQuery I have tried to use but nothing is happening which leads me to believe the symbol is not being detected. 这是我尝试使用的jQuery,但是什么也没发生,这使我相信未检测到该符号。

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

           }

});

Assuming your code is saved as UTF-8 you can just use the character as a string 假设您的代码另存为UTF-8,则可以将字符用作字符串

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

OR you could do it by character code 或者您可以通过字符代码来完成

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

and for extra credit... replace all occurences. 为了额外的信誉...替换所有出现的情况。

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

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

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

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

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

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