jQuery获取元素的属性和值(jQuery get attributes and values of element)

编程入门 行业动态 更新时间:2024-10-25 18:28:54
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=value

Is 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

本文发布于:2023-08-06 20:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   元素   jQuery   element   values

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!