查找具有特定数据属性jQuery的元素 [英] Find an element with a specific data attribute jQuery

查看:124
本文介绍了查找具有特定数据属性jQuery的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我总是可以迭代DOM树并检查数据字段的每个元素,如果值是正确的,但我想要一个更清晰的解决方案。例如

I understand that I can always iterate down the DOM tree and check every element for a data field and if the value is correct but I would like a cleaner solution. For example

    <div id="theDiv">
       <div>
         <span data-num="3" ></span>
         OTHER HTML
       </div>
       <div>
         <div><span data-num="23"></span><div>
         OTHER HTML
       </div>
       <div>
         <span data-num="3"></span>
         OTHER HTML
       </div>
    </div>

是否有一个jQuery one liner来查找data-num = 3的所有跨度?我知道我可以找到所有跨度

Is there a jQuery one liner to find all spans with data-num=3? I know I can find all spans by

  $("#theDiv").find(span).each(function(e) { ... });

但我想要像

  $("#theDiv").find(span > data-num=3).each(function(e) { ... });


推荐答案

使用属性等于选择器

 $("#theDiv span[data-num='3']")

试一试!

这篇关于查找具有特定数据属性jQuery的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆