jquery count elements with attribute

前端 未结 3 1560
南笙
南笙 2020-12-10 11:58

Can I count the number of elements with a specific attribute?

For example all the images with the src attribute test.gif

相关标签:
3条回答
  • 2020-12-10 12:38

    You want size() so something like:

    something like:

    $('img[src="test.gif"]').size();
    
    0 讨论(0)
  • 2020-12-10 12:58

    if you want to check the presence of the attribute only

    $('img[src]').length //or .size() 
    
    0 讨论(0)
  • 2020-12-10 13:04

    Use the CSS attribute selectors to filter on the attribute

    $("img[src='test.gif']").length;
    

    the size() and length both returns the number of elements matched;

    0 讨论(0)
提交回复
热议问题