检测HTML元素是否包含特定字符实体 [英] Detect whether HTML element contains a specific character entity
问题描述
如果我有这样的标记:
<div id="foo"></div>
我想稍后检测div#foo是否仍然包含相同的字符实体,我想要能够通过将它与& #xf067;
进行比较而不是
(在我的代码中)基础对于维护来说相当迟钝。)
and I want to detect later whether div#foo still contains that same character entity, I'd like to be able to do so by comparing it to 
rather than to
(which in my code base is rather obtuse for maintenance purposes).
我尝试过这样的事情(使用jQuery):
I've tried things like this (using jQuery):
console.log($('<textarea />').html($('#foo').html()).val());
但这似乎仍然输出了一个漂亮的小方块你说什么''回合'而不是所需的& #xf067;
。
But that seems to still output the nice little square "what you talkin' 'bout" character rather than the desired 
.
我对纯JavaScript或特定于jQuery的解决方案持开放态度。
I'm open to plain JavaScript or jQuery-specific solutions.
推荐答案
您可以在JavaScript中使用Unicode实体。例如:
You can use a Unicode entity in JavaScript. For example:
(HTML:< div id ='foo'>& #xf067;< / div>
)
JavaScript:
JavaScript:
console.log($('#foo').html().charCodeAt(0).toString(16));
//=> f067
console.log($('#foo').html().indexOf('\uf067'));
//=> 0
这篇关于检测HTML元素是否包含特定字符实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!