使用jQuery获取元素的所有属性

Sty*_*hon 121 javascript jquery attributes

我试图通过一个元素并获取该元素的所有属性来输出它们,例如标记可能有3个或更多属性,我不知道,我需要获取这些属性的名称和值.我正在思考以下问题:

$(this).attr().each(function(index, element) {
    var name = $(this).name;
    var value = $(this).value;
    //Do something with name and value...
});
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我这是否可能,如果是这样,那么正确的语法是什么?

pim*_*vdb 236

attributes属性包含所有:

$(this).each(function() {
  $.each(this.attributes, function() {
    // this.attributes is not a plain object, but an array
    // of attribute nodes, which contain both the name and value
    if(this.specified) {
      console.log(this.name, this.value);
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

您还可以做的是扩展,.attr以便您可以调用它.attr()来获取所有属性的普通对象:

(function(old) {
  $.fn.attr = function() {
    if(arguments.length === 0) {
      if(this.length === 0) {
        return null;
      }

      var obj = {};
      $.each(this[0].attributes, function() {
        if(this.specified) {
          obj[this.name] = this.value;
        }
      });
      return obj;
    }

    return old.apply(this, arguments);
  };
})($.fn.attr);
Run Code Online (Sandbox Code Playgroud)

用法:

var $div = $("<div data-a='1' id='b'>");
$div.attr();  // { "data-a": "1", "id": "b" }
Run Code Online (Sandbox Code Playgroud)

  • `attributes`集合包含旧IE中的所有可能属性,而不仅仅是那些已在HTML中指定的属性.您可以使用每个属性`specified`属性过滤属性列表来解决此问题. (11认同)
  • 对于jQuery` .attr()`方法,这是一个非常好的预期功能.奇怪的是jQuery不包含它. (5认同)

has*_*nge 25

以下是我可以做的许多方法的概述,供我自己参考以及你的:)函数返回属性名称及其值的哈希值.

香草JS:

function getAttributes ( node ) {
    var i,
        attributeNodes = node.attributes,
        length = attributeNodes.length,
        attrs = {};

    for ( i = 0; i < length; i++ ) attrs[attributeNodes[i].name] = attributeNodes[i].value;
    return attrs;
}
Run Code Online (Sandbox Code Playgroud)

带有Array.reduce的Vanilla JS

适用于支持ES 5.1(2011)的浏览器.需要IE9 +,在IE8中不起作用.

function getAttributes ( node ) {
    var attributeNodeArray = Array.prototype.slice.call( node.attributes );

    return attributeNodeArray.reduce( function ( attrs, attribute ) {
        attrs[attribute.name] = attribute.value;
        return attrs;
    }, {} );
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

此函数需要jQuery对象,而不是DOM元素.

function getAttributes ( $node ) {
    var attrs = {};
    $.each( $node[0].attributes, function ( index, attribute ) {
        attrs[attribute.name] = attribute.value;
    } );

    return attrs;
}
Run Code Online (Sandbox Code Playgroud)

下划线

也适用于lodash.

function getAttributes ( node ) {
    return _.reduce( node.attributes, function ( attrs, attribute ) {
        attrs[attribute.name] = attribute.value;
        return attrs;
    }, {} );
}
Run Code Online (Sandbox Code Playgroud)

lodash

甚至比Underscore版本更简洁,但仅适用于lodash,而不适用于Underscore.需要IE9 +,IE8中有错误.感谢@AlJey 那个.

function getAttributes ( node ) {
    return _.transform( node.attributes, function ( attrs, attribute ) {
        attrs[attribute.name] = attribute.value;
    }, {} );
}
Run Code Online (Sandbox Code Playgroud)

测试页面

在JS Bin,有一个涵盖所有这些功能的实时测试页面.该测试包括布尔属性(hidden)和枚举属性(contenteditable="").