gpt4 book ai didi

jquery - 如何获取 div Jquery 的所有后代

转载 作者:行者123 更新时间:2023-12-01 06:22:10 26 4
gpt4 key购买 nike

我需要获取 div en Jquery 的所有后代子级,而不仅仅是直接子级我需要属性 css 应用于元素 p 和元素 span

<!DOCTYPE html>
<html>

<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").children().css({
"color": "red",
"border": "2px solid red"
});
});
</script>
</head>

<body>

<div class="descendants" style="width:500px;">div (current element)
<p>p (child)
<span>span (grandchild)</span>
</p>
<p>p (child)
<span>span (grandchild)</span>
</p>
</div>

</body>

</html>

最佳答案

使用 .find('*') 而不是 .children() (它只获取...等待...元素的子元素) :

$(document).ready(function() {
$("div").find('*').css({
"color": "red",
"border": "2px solid red"
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="descendants" style="width:500px;">div (current element)
<p>p (child)
<span>span (grandchild)</span>
</p>
<p>p (child)
<span>span (grandchild)</span>
</p>
</div>

关于jquery - 如何获取 div Jquery 的所有后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com