问题
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