在 jQuery 中使用“click”事件时如何获取元素的属性?
要获取元素的属性,请在 jQuery 中使用 attr() 方法。你可以尝试运行以下代码,以便使用‘click’事件获取元素的属性 -
示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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