gpt4 book ai didi

javascript - 无法使用jquery获取元素的属性

转载 作者:行者123 更新时间:2023-11-28 13:46:38 25 4
gpt4 key购买 nike

我有这个html:

<img src="http://localhost:82/Clone//images/hosts/Kinx_9843a.jpg" data-name="/images/hosts/K_9843a.jpg" alt="">

我正在尝试得到这个:

$('body').on('click','.hostPic',function(){ 
console.log($(this).attr('data-name'));
});

每当按下图片时,我都想获取它的 data-name 属性,但它不起作用。当我尝试检索该属性时,我收到 undefined 报告。有什么原因吗?

最佳答案

hostPic 类添加到该图像。

此外,您可以只使用.data('name'),但实际上并没有什么区别。较旧的浏览器可能会遇到 .data 问题,但这对我来说从来都不是问题。

关于javascript - 无法使用jquery获取元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13842074/

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