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