开发者

output all set attributes of an element [duplicate]

This question already has answers here: Get all attributes of an element using jQuery (8 answers) 开发者_JS百科 Closed 8 years ago.

I have a jquery object which represents a input button element on the page. How can I with jquery to output via console.log all properties/attributes of this element?


Assuming the HTML of the page is

<body>
  <img id="smile" class="big" alt="smile" madeupattribute="yep" src="http://mikegrace.s3.amazonaws.com/forums/stack-overflow/smile.png"/>
</body>

you could do

var domElement = $("img")[0] // [0] returns the first DOM element that jQuery found
$(domElement.attributes).each(function(index, attribute) {
  console.log("Attribute:"+attribute.nodeName+" | Value:"+attribute.nodeValue);
});

Example page => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-get-element-attributes-jquery.html

Example page console output

output all set attributes of an element [duplicate]


If you want just HTML attributes:

var e = document.getElementById('my_input');
for (var x in e)
  if (e.hasAttribute(x))
    console.log(x);

If you want all properties that can be retrieved/set via JavaScript:

var e = document.getElementById('my_input');
for (var x in e)
  if (typeof e[x] != 'function')
    console.log(x);

Example on JSBin -- for some reason, Firefox fails halfway through the "all properties" list when trying to compute typeof e['selectionStart'].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜