Detect whether HTML element contains a specific character entity

走远了吗. 提交于 2019-12-04 06:41:08

问题


If I have markup like this:

<div id="foo">&#xf067;</div>

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 &#xf067; rather than to (which in my code base is rather obtuse for maintenance purposes).

I've tried things like this (using jQuery):

console.log($('<textarea />').html($('#foo').html()).val());

But that seems to still output the nice little square "what you talkin' 'bout" character rather than the desired &#xf067;.

I'm open to plain JavaScript or jQuery-specific solutions.


回答1:


You can use a Unicode entity in JavaScript. For example:

(HTML: <div id='foo'>&#xf067;</div>)

JavaScript:

console.log($('#foo').html().charCodeAt(0).toString(16));
  //=> f067
console.log($('#foo').html().indexOf('\uf067'));
  //=> 0

Here's a JSFiddle.



来源:https://stackoverflow.com/questions/25408731/detect-whether-html-element-contains-a-specific-character-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!