JQuery“包含”对html编码没有任何回报 [英] JQuery "contains" returns nothing for html encoding
问题描述
var foundin = $('*:contains("the")').last();
在页面上工作,但
var foundin = $('*:contains("©")').last();
什么都不返回,即使'& copy'也出现在页面源中。我试图逃避'&',但这也不行。有没有办法使用包含在页面中找到一个html编码的字符?
returns nothing, even though '©' does appear in the page source. I tried escaping the '&', but that doesn't work either. Is there a way to use contains to find an html encoded character in a page?
基本上,我想做的是找到元素包含©并解析以获取版权所有者。我在这里询问了一个与正则表达式相似的问题:选择HTML文本元素与正则表达式?
Basically, what I want to do is find the element contains © and parse it to get the copyright holder. I asked a similar question aiming at this with regex here: select HTML text element with regex?
推荐答案
快速而肮脏的
var $div = $("<div>");
$.expr[":"].containsEnc = function(a, b, c, d) {
var decoded = $div.html(c[3]).text(); // decode © to ©
return ~a.textContent.indexOf(decoded); // does the element contain the character
};
然后可以使用 containsEnc
作为选择器:
Then you can use containsEnc
as a selector:
$('body:containsEnc("©")').html('ok');
尽管如此,它可能不是最安全,最性价比的。
It may not be the safest and most performant, though.
这篇关于JQuery“包含”对html编码没有任何回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!