counting total numbers of div with jquery
how can i count the total number of divs with same name.......i.e.
suppose i have 3 divs with same name than 开发者_StackOverflow中文版how can i count these three divs with jquery.
This gives total number of <div>
elements on the page:
var count = $('div').length
Like this:
var cnt = $('div[name=asfd]').length;
However, the name property is not a standard attribute for the div element.
If you are looking for a text inside the div elements:
var cnt = $('div').filter(function(){
return $(this).text() == 'John Doe'
}).length;
You can do any comparison you like in the filter. Note that this will check all div elements in the document, so you should try to limit the number of elements in the first expression if possible.
Very easy +)
$('div').size()
Or specify any additional selector parameters.
精彩评论