jQuery获取元素的属性和值(jQuery get attributes and values of element)
说我有这个HTML代码:
<img id="idgoeshere" src="srcgoeshere" otherproperty="value" />我可以通过它的id引用该元素: $('#idgoeshere'))现在,我想获取该元素上的所有属性及其值:
src=srcgoeshere otherproperty=value有没有什么办法可以通过编程方式使用jQuery和/或纯Javascript?
Say I have this HTML code:
<img id="idgoeshere" src="srcgoeshere" otherproperty="value" />I can reference the element by its id: $('#idgoeshere')) Now, I want to get all the properties and their values on that element:
src=srcgoeshere otherproperty=valueIs there some way I can do that programmatically using jQuery and/or plain Javascript?
最满意答案
您可以通过检查属性属性来获取列表:
var attributes = document.getElementById('idgoeshere').attributes; // OR var attributes = $('#idgoeshere')[0].attributes; alert(attributes[0].name); alert(attributes[0].value); $(attributes).each(function() { // Loop over each attribute });You can get a listing by inspecting the attributes property:
var attributes = document.getElementById('idgoeshere').attributes; // OR var attributes = $('#idgoeshere')[0].attributes; alert(attributes[0].name); alert(attributes[0].value); $(attributes).each(function() { // Loop over each attribute });更多推荐
src,id,value,otherproperty,电脑培训,计算机培训,IT培训"/> <meta name="de
发布评论