如何获取:contains()选择器的总实例

时间:2014-08-22 02:49:58

标签: jquery jquery-selectors contains

我有这些div

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

我的jquery包含选择器

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

如果匹配(即Content1),则将显示所有匹配的DIV。

问题:有没有办法知道发生了多少场比赛?对于“Content1”,它应该说3条记录。此外,如果没有匹配,则会显示“0结果”。

1 个答案:

答案 0 :(得分:0)

这是一个简单的例子。我希望这会有所帮助:)

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

   alert(matches);

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

相关问题