在 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>

更新時間:14-Feb-2020

2K+ 次觀看

開啟您的 職業

完成課程獲取認證

立即開始
廣告