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

Viv*_*iya 5 jquery

我无法获取 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()"/>
Run Code Online (Sandbox Code Playgroud)

这里没有显示在警报框中..你能帮我吗??

Fel*_*lix 6

请记住将您的代码包装在其中$(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>
Run Code Online (Sandbox Code Playgroud)

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