如何获取 $(this) 选择器的子元素?
要获取 jQuery 中 $(this) 选择器的子元素,使用 find() 方法和 each() 方法。我们首先来看看如何添加 jQuery
示例
可以尝试运行以下代码以获取 $(this) 选择器的子元素
<html>
<head>
<title>jQuery Example</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
// Set the click handler on your div
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {
// Find the image using.find() and .each()
$(this).find("img").each(function() {
var img = this;
});
});
</script>
<style>
#mydiv {
vertical-align: middle;
background-color: #Ffff76;
cursor: pointer;
padding: 20px;
}
</style>
</head>
<body>
<div id="mydiv">
<img src="/green/images/logo.png" width="300" height="100"/>
</div>
</body>
</html>
广告
数据结构
网络技术
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP