检测HTML元素是否包含特定字符实体 [英] Detect whether HTML element contains a specific character entity

查看:268
本文介绍了检测HTML元素是否包含特定字符实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的标记:

<div id="foo">&#xf067;</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 &#xf067; 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 &#xf067;.

我对纯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

这是一个JSFiddle。

这篇关于检测HTML元素是否包含特定字符实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆