开发者

Selecting div with a certain number of children

I want to select every div开发者_JAVA百科 of a certain class with more than 14 spans inside. How can I do this?


$('div').filter(function(){
    return $(this).find('span').size() > 14
});

I'm not sure you can select on the count of elements, unfortunately.

Here's a fiddle in action.


var $divs = $('div span:nth-child(15)').parents("div");

Here is a fiddle: http://jsfiddle.net/brian3f/gK6eG/2/


Not the most elegant, but this works. Used limit of 2 to make it easier on my fiddle typing:

html:

<div id="div2">
    <span></span>
    <span></span>
</div>

<div id="div3">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>

<div id="results"></div>

js:

var the_divs = [];
$( 'div' ).each(function(){
   if( $( this).children('span').length > 2 )
       the_divs.push( $( this ).attr('id') );
});

$( 'div#results' ).text( the_divs.join( ', ' ) );

result:

div1, div3

fiddle: http://jsfiddle.net/JBA9y/


$("div.class").filter(function(){ return $("span",this).length===14}).addClass("yahoo");

here is the fiddle http://jsfiddle.net/E86e5/4/


You can use jquery size or length for refrence and example refer http://api.jquery.com/size/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜