gpt4 book ai didi

Javascript 循环遍历图像属性

转载 作者:行者123 更新时间:2023-12-02 16:48:29 25 4
gpt4 key购买 nike

我的网站上有一个 div,其中包含许多图像。我正在尝试获取所述 div 内每个图像的 onclick 属性。

<div class="hoi">
<img src="" onclick="alert('hoi')"/>
<img src="" onclick="alert('hoi')"/>
</div>

我尝试了以下 Javascript 代码:

var count = $(".hoi img").length;

for(var i = 0; i <= count; i++){
alert($('.hoi').find('img').getAttribute('onclick'));
}

但是我收到以下错误Uncaught TypeError: undefined is not a function

这是一个 fiddle http://jsfiddle.net/4Lhn0u87/7/

最佳答案

jQuery 对象中没有名为 getAttribute() 的方法,您有 .attr()getAttribute()是一个 dom api 方法。

$('.hoi img').each(function(){
console.log($(this).attr('onclick'))
})

要迭代一组元素,您可以使用 .each() .

关于Javascript 循环遍历图像属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26905576/

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