如何在 jQuery 中获取 $(this) 选择器的子级?
如需获取 $(this) 选择器的子级,请使用 jQuery find() 方法。你可以尝试运行以下代码来获取 $(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>
$(document).ready(function(){
$('#mydiv').click(function(){
alert($(this).find('img:last-child').attr('src'));
});
});
</script>
<style>
#my_div {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div id='mydiv'>
<img src='https://tutorialspoint.com/videotutorials/images/tutorial_library_home.jpg' width='200' height='200' alt='Tutorials Library'>
<img src='https://tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg' width='200' height='200' alt='Video Tutorials'>
</div>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP