jQuery - select all elements with attribute name (not value) beginning with…?

前端 未结 2 1708
礼貌的吻别
礼貌的吻别 2020-12-11 21:06

Say I\'m looking for all elements with an attribute \'data-language\', whose value begins with \'java\' (would match both \'java\' and \'javascript\'). I know how to do this

相关标签:
2条回答
  • 2020-12-11 21:22

    jQuery has a nice function for this.

    http://api.jquery.com/data/

    $("div").data() // returns all data attributes to this div
    
    0 讨论(0)
  • 2020-12-11 21:38

    There is no shortcut, you may use the attributes collection :

     $(someselector).filter(function(){
          var attrs = this.attributes;
          for (var i=0; i<attrs.length; i++) {
              if (attrs[i].name.indexOf("someStartOfName")==0) return true;
          }         
          return false;
     });
    
    0 讨论(0)
提交回复
热议问题