0

As simple as the title states, i want to get all values for for elements that contain a specific data-attribute. Any solutions I've found have been directly related to querying the value of the attribute which is not what I want.

So for example - if I have multiple elements with the attribute data-help-id how do I retrieve all those attributes either as an array or an object?

4

1 回答 1

2

我将使用.map将包含具有所需属性的元素的 jQuery 对象转换为值数组:

var $elements = $('[data-help-id]')
    , values = $elements.map(function () {
          return $(this).data('help-id');
      }).get();

示例:http: //jsfiddle.net/68kcg/

于 2013-07-24T15:02:14.070 回答