javascript - .html() 未注册 ASCII 符号

标签 javascript html

我这里有一个函数,可以在单击时在不同的 ASCII 符号(特别是 ⊕ (⊕)&ominus (⊖))之间切换。我不明白的是它变成 之后它不会变回来。有谁知道为什么吗?

这会生成表格行

"<tr><td>Other</td><td>" + stats.others.length + <span class='displaybutton' style='cursor:pointer;'>&oplus;</span></td></tr>";

重要的部分是这个<span>

<span class='displaybutton' style='cursor:pointer;'>&oplus;</span>

这应该在符号之间切换

$('.displaybutton').click(function(){
    $('#otherResponseList').toggle(); 
    if($(this).html() == '&ominus;'){
        $(this).html('&oplus;');
    } else {
        $(this).html('&ominus;');
    }
});

最佳答案

这两个符号不是 ASCII。它们是 HTML 实体,属于 Unicode 字符集。它们不属于 ASCII 字符集。

比较 HTML 实体的 html 表示可能很棘手。它们可以用实体来表示,但也可以转换为相应的Unicode字符。解决方案是考虑结果,即文本内容。使用 jQuery.text() 和 Unicode 代码点可以轻松操作文本内容到 ominus (0x2296) 和 oplus (0x2295):

$('.displaybutton').click(function(){
    $('#otherResponseList').toggle(); 
    $(this).text($(this).text()=='\u2296'?'\u2295':'\u2296');
});

关于javascript - .html() 未注册 ASCII 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42427716/

相关文章:

javascript - AngularJS $http.post() : returns ERR_EMPTY_RESPONSE

javascript - jQuery 脚本无法在 Internet Explorer 中运行(第 1 行第 1 个字符出现语法错误)

javascript - 推送到 vuex 存储数组在 VueJS 中不起作用

javascript - 在浏览器中以半页增量垂直放置嵌入的 PDF

html - 行在 Bootstrap 中被长列压低

css - Canvas 扭曲绘图。如何获得设置大小和样式大小之间的比例因子?

javascript - 单选按钮 onchange/onclick 事件无法正常工作

javascript - 无法读取未定义的 IndexedDB 的属性 'transaction'

javascript - 在 d3.js 元素中添加 slider

python - 在 Bokeh 中使用自定义 html 工具提示时如何抑制科学计数法?