在jQuery中获取字符符号的HTML实体 [英] Get HTML entity of character symbol in jQuery
问题描述
如果一个平方根符号存在于我喜欢把它涂成绿色.所以我有这样的事情:
foo<div class="cell">bar</div><div class="cell">√</div><div class="cell">foo</div><div class="cell">√</div>
我对 jQuery 的尝试:
$(".value_spec_table_description_div_cs").each(function(){if ($(this).html() === "√"){$(this).css("color", "rgba(0,128,0,1.0)");}});
不幸的是,html() 似乎没有转换符号 √再次进入 HTML 实体,因此该语句永远不会返回 true.
我尝试了很多:
var test = $("#table").html();var html = $("").html(test).html();警报(html);
每个特殊字符都在实体中,除非方根符号√变成√
.我该如何解决这个问题?
浏览器在将 HTML 转换为 DOM 时对其进行规范化.如果没有发出 Ajax 请求以获取页面的源代码、编写自定义解析器、找到您关心的 HTML 以及比较那个,您就无法真正做到这一点.
最好只比较文本:
if ($(this).text() === "√"){
If a square root symbol exists in a I like to color it green. So I have something like this:
<div class="cell">foo</div>
<div class="cell">bar</div>
<div class="cell">√</div>
<div class="cell">foo</div>
<div class="cell">√</div>
My attempt with jQuery:
$(".value_spec_table_description_div_cs").each(function(){
if ($(this).html() === "√"){
$(this).css("color" , "rgba(0,128,0,1.0)");
}
});
Unfortunately it seems html() is not converting the symbol √ into HTML entity again so the statement never returns true.
I tried a lot like:
var test = $("#table").html();
var html = $("<textarea/>").html(test).html();
alert(html);
Every special character is in entity unless the root square symbol √ into √
. How can I solve this?
Browsers normalise HTML when they convert it to a DOM. Short of making an Ajax request to get the source code of the page, writing a custom parser, finding the HTML you care about, and comparing that you can't really do this.
Better to just compare text:
if ($(this).text() === "√"){
这篇关于在jQuery中获取字符符号的HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!