开发者

counting total numbers of div with jquery

开发者 https://www.devze.com 2022-12-14 23:05 出处:网络
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 nu

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.

0

精彩评论

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