How to get the total instance of the :contains() Selector

你离开我真会死。 提交于 2019-12-23 06:12:14

问题


I have these divs

<div id="content" style="display:none">**Content1 goes here**</div>
<div id="content" style="display:none">Content2 goes here</div>
<div id="content" style="display:none">Content3 goes here</div>
<div id="content" style="display:none">Content4 goes here</div>
<div id="content" style="display:none">**Content1 goes here too**</div>
<div id="content" style="display:none">**Content1 goes here again**</div>
<div id="content" style="display:none">Content4 goes here part 2</div>

I have my jquery contains selector

$( "div:contains('Content1')" ).css( "display", "block" );

If there's a match (i.e. Content1), then all matched DIVs will be displayed.

QUESTION: Is there a way to know how many matches occurred? For "Content1", it should say 3 records. Also, if there's no match, it will say "0 results".


回答1:


Here is a simple example. I hope this helps :)

  var matches = $("div:contains('Content1')").length;

   alert(matches);

http://jsfiddle.net/silkherb/rvecpu5d/2/



来源:https://stackoverflow.com/questions/25438855/how-to-get-the-total-instance-of-the-contains-selector

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