gpt4 book ai didi

jquery - 如何获取img标签的alt属性值?

转载 作者:行者123 更新时间:2023-12-01 00:18:26 25 4
gpt4 key购买 nike

无法获取img标签的alt属性值

下面是获取img标签的alt属性值

<html>
<head>
<script>

$('img').click(function () {
var alt = $(this).attr("alt")
alert(alt);
});

</script>
</head>

<img src="white.png" width="25px" height="25px" alt="1" id="star1" onmouseover="starOverOne()" onmouseout="starOutOne()"/>

这里警报框中没有显示任何内容..你能帮我吗?

最佳答案

记住将您的代码包装在 $(document).ready(function() {...}); 中并包含您的 jQuery 库。尝试遵循以下基本 HTML 结构:

<html>
<head>

</head>

<body>
<img src="white.png" width="25px" height="25px" alt="1" id="star1" onmouseover="starOverOne()" onmouseout="starOutOne()"/>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').click(function () {
var alt = $(this).attr("alt")
alert(alt);
});
});
</script>
</body>
</html>

演示:http://jsfiddle.net/aHRN7/

关于jquery - 如何获取img标签的alt属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20861552/

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