【发布时间】:2011-11-26 03:11:45
【问题描述】:
正如标题中所说,我想找到类似:contains() 的东西,但要匹配一个精确的字符串。换句话说,它不应该是部分匹配。例如在这种情况下:
<div id="id">
<p>John</p>
<p>Johny</p>
</div>
$("#id:contains('John')") 将匹配 John 和 Johny,而我只想匹配 John。
提前致谢。
编辑: ES2015 单线解决方案,以防有人寻找:
const nodes = [...document.querySelectorAll('#id > *')].filter(node => node.textContent === 'John');
console.log(nodes);
/* Output console formatting */
.as-console-wrapper { top: 0; }
.as-console { height: 100%; }
<div id="id">
<p>John</p>
<p>Johny</p>
</div>
【问题讨论】:
-
jQuery text match的可能重复
标签: javascript jquery jquery-selectors