在 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>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP