简体   繁体   English

jQuery-选择所有以...开头的属性名称(不是值)的元素?

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

Say I'm looking for all elements with an attribute 'data-language', whose value begins with 'java' (would match both 'java' and 'javascript'). 假设我正在寻找具有属性“数据语言”的所有元素,其值以“ java”开头(将同时匹配“ java”和“ javascript”)。 I know how to do this: 我知道该怎么做:

$('[data-language^="java"]')

But my question is: how do I get all elements that have an attribute ( not the value of the attribute, but the actual attribute name itself ) beginning with something? 但是我的问题是:如何获取具有某个属性( 不是属性的值,而是实际的属性名称本身 )的所有元素,且从某些东西开始? For example: 例如:

  1. all elements that have an attribute name beginning with "data-s", or 具有以“ data-s”开头的属性名称的所有元素,或者
  2. all elements that have data attributes at all (attributes beginning with "data-"). 所有具有数据属性的元素(属性以“ data-”开头)。

There is no shortcut, you may use the attributes collection : 没有捷径,您可以使用attributes集合:

 $(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;
 });

jQuery has a nice function for this. jQuery为此提供了一个不错的功能。

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

$("div").data() // returns all data attributes to this div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择具有class属性且其值完全以给定字符串开头的元素(使用jQuery)? - How to select elements (using jQuery) that have the class attribute with a value beginning exactly with a given string? 通过名称属性javascript选择嵌套元素(无jquery) - Select nested elements by name attribute javascript(no jquery) 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value 如何在jQuery中选择所有具有匹配属性的元素? - how to select all elements with matching attribute of this in jQuery? 选择所有具有特定属性值的首个元素 - Select all first elements with specific attribute value jQuery选择具有相同类名的所有元素 - jQuery select all elements with same class name 使用jQuery通过名称属性获取选择值 - Get select value by name attribute using jQuery Jquery找到自定义属性的所有元素开头,获取其余的名称及其值 - Jquery find all elements with custom attribute begin with, get rest of it's name and it's value jQuery选择具有名称属性的多个元素的属性值 - jQuery selecting attribute value of multiple elements with name attribute Select 不使用 jQuery 的所有具有“data-xxx”属性的元素 - Select all elements with a "data-xxx" attribute without using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM