<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script type="text/javascript">
$(function(){
$('.parent').children('.children1');//父元素获取某个类名的子元素
let sibling = $('.children2').siblings()[0];//子元素获取同级某个兄弟元素
let child = $('.children1').closest('.parent').children('.children3');//间接通过父元素找到某个子元素(parents找到父级根元素)
});
</script>
</head>
<body>
<div id="container">
<div class="parent">
<div class="children1">1</div>
<div class="children2">2</div>
<div class="children3">3</div>
</div>
</div>
</body>
</html>
jquery根据类名或标签获取子元素
最新推荐文章于 2024-04-15 20:23:43 发布