Ent*_*ity 1 javascript jquery jquery-selectors
说我有这个HTML代码:
<img id="idgoeshere" src="srcgoeshere" otherproperty="value" />
Run Code Online (Sandbox Code Playgroud)
我可以通过它的id引用元素:$('#idgoeshere'))现在,我想获得该元素上的所有属性及其值:
src=srcgoeshere
otherproperty=value
Run Code Online (Sandbox Code Playgroud)
有没有办法我可以使用jQuery和/或纯Javascript以编程方式执行此操作?
您可以通过检查attributes属性来获取列表:
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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11830 次 |
| 最近记录: |