如何获取 $(this) 选择器的子元素?
jqueryweb developmentfront end technology
要在 jQuery 中获取 $(this) 选择器的子元素,请使用 find() 方法和 each() 方法。我们先来看看如何添加 jQuery:
示例
您可以尝试运行以下代码来获取 $(this) 选择器的子元素:
<html>
<head>
<title>jQuery Example</title>
<script src = "https://ajax.googleapis.com/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>

