在jQuery中获取HTML个字符符号实体
Get HTML entity of character symbol in jQuery
如果 a 中存在平方根符号,我喜欢将其涂成绿色。所以我有这样的东西:
<div class=”cell”>foo</div>
<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 实体,因此该语句永远不会 returns 为真。
我尝试了很多,例如:
var test = $("#table").html();
var html = $("<textarea/>").html(test).html();
alert(html);
每个特殊字符都在实体中,除非根方块符号√变为√
。我该如何解决?
浏览器在将 HTML 转换为 DOM 时将其规范化。只需发出 Ajax 请求以获取页面源代码、编写自定义解析器、找到您关心的 HTML 并比较 that 即可真的这样做。
最好只比较文本:
if ($(this).text() === "√"){
就像@Quentin 说的。 jQuery 看到的是渲染的字符 '√'
而不是 '√'
。你要么将它与呈现的字符进行比较,要么将其与 unicode 等效 '\u221A'
,如下所示:
$('.cell').each(function () {
if ($(this).text() === '\u221A') {
$(this).css('color', 'rgba(0, 128, 0, 1.0)');
}
});
如果 a 中存在平方根符号,我喜欢将其涂成绿色。所以我有这样的东西:
<div class=”cell”>foo</div>
<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 实体,因此该语句永远不会 returns 为真。
我尝试了很多,例如:
var test = $("#table").html();
var html = $("<textarea/>").html(test).html();
alert(html);
每个特殊字符都在实体中,除非根方块符号√变为√
。我该如何解决?
浏览器在将 HTML 转换为 DOM 时将其规范化。只需发出 Ajax 请求以获取页面源代码、编写自定义解析器、找到您关心的 HTML 并比较 that 即可真的这样做。
最好只比较文本:
if ($(this).text() === "√"){
就像@Quentin 说的。 jQuery 看到的是渲染的字符 '√'
而不是 '√'
。你要么将它与呈现的字符进行比较,要么将其与 unicode 等效 '\u221A'
,如下所示:
$('.cell').each(function () {
if ($(this).text() === '\u221A') {
$(this).css('color', 'rgba(0, 128, 0, 1.0)');
}
});