【问题标题】:jquery count elements with attributejquery计算具有属性的元素
【发布时间】:2010-01-08 20:57:14
【问题描述】:

我可以计算具有特定属性的元素的数量吗?

例如所有src属性为test.gif的图片

【问题讨论】:

    标签: jquery count


    【解决方案1】:

    使用 CSS attribute selectors 过滤属性

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

    size()length都返回匹配的元素个数;

    【讨论】:

      【解决方案2】:

      如果你只想检查属性是否存在

      $('img[src]').length //or .size() 
      

      【讨论】:

        【解决方案3】:

        你想要 size() 这样的东西:

        类似:

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

        【讨论】:

        • .length 是首选,因为它是属性与函数调用。在大循环中,length 的性能会更好。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多