如何在 jQuery 中使用"click"事件获取元素的属性?

jqueryweb developmentfront end technology

要获取元素的属性,请使用 jQuery 中的 attr() 方法。您可以尝试运行以下代码,使用"click"事件获取元素的属性。

示例

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("Width of image: " + $("img").attr("width"));
    });
});
</script>
</head>
<body>

<img src="/videotutorials/images/coding_ground_home.jpg" alt="Coding Ground" width="284" height="280"><br>

<button>Get Width</button>

</body>
</html>

相关文章