【发布时间】:2017-06-26 16:51:40
【问题描述】:
我正在尝试使用 :contains 在 div 中查找文本位,它工作正常,除非遇到包含“ ”或“'”的文档。
奇怪的是,我甚至可以对包含“&”的文本使用 :contains,但不能用于其他情况。
这是我的尝试,也是https://jsfiddle.net/sesyj5kd/
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="well">
<div id="dText" style="background: white; height: 90%; overflow-y:auto;" ></div>
</div>
</div>
</div>
</div>
脚本:
var str1 = "<html><head></head><body><p>This is the doc's first sentence</p><div>not the first sentence, but it's in a div & all </div> </body></html>";
$("#dText").html( str1 );
console.log( $("#dText").html() );
console.log( $("*:contains('the doc's first')") );
console.log( $("*:contains('in a div & all')") );
第二个 :contains 找到指定的文本,但第一个没有,即使我搜索“doc\'s”;是否有必要/可能转义   之类的字符?
主要问题我面临的是 div.html(htmlString) 正确显示内容,但是查看 console.log(div.html()) 我可以看到很多   (即使这些不在 HTML 字符串中)当目标文本在单词之间有   时,这会阻止 :contains 在任何多词查询上正常工作。
【问题讨论】:
标签: javascript jquery