如何使用 JQuery 中的“click”事件獲取元素屬性?


要獲取元素屬性,請在 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>

更新於: 2020 年 2 月 14 日

2K+ 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

入門
廣告