gpt4 book ai didi

jquery - 如何使用jquery的每个函数来获取每个元素的attr

转载 作者:行者123 更新时间:2023-12-01 08:28:32 25 4
gpt4 key购买 nike

html 是

<table>
<tr>
<td><img src="ex.jpg" id="image" class="edit" name="n1"></td>
<td><img src="ex.jpg" id="image" class="edit" name="n2"></td>
<td><img src="ex.jpg" id="image" class="edit" name="n3"></td>
<td><img src="ex.jpg" id="image" class="edit" name="n4"></td>
</tr>
</table>

Jquery 代码是:

$(".edit").click(function() {

$("#image").each(function(){

alert(this.name);

});

});

这里,它只提醒一个名字,我的意思是只提醒“n1”。

我如何提醒名称属性。每个元素?

谢谢

最佳答案

多个元素不能具有相同的 ID(因此得名:ID)。如果这样做,#image 始终仅匹配具有给定 ID 的第一个元素(在您的情况下为 #n1 图像)。但是,您可以向一个元素添加多个类:

HTML:

<img class="edit image" name="foobar" />

JS:

$(".image").each(function(){
alert($(this).attr('name'));
});
在此上下文中的

this 是普通 DOM 元素。您首先必须将其更改为 jQuery 对象 (var $that = $(this)),然后才能对其使用 .attr()

关于jquery - 如何使用jquery的每个函数来获取每个元素的attr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1686009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com