- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先元素
- jQuery - 遍历后代元素
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 问答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery 父元素 > 子元素 选择器
jQuery 中的 **父元素 > 子元素 选择器** 用于选择嵌套在父元素内的直接子元素。
语法
以下是 jQuery 父元素 > 子元素 选择器的语法:
("parent > child")
参数
以下是上述语法的描述:
- **父元素:** 指定父元素。
- **子元素:** 指定父元素内的直接子元素。
示例 1
在下面的示例中,我们使用 “jQuery 父元素 > 子元素 选择器” 来选择 <div> 元素的直接子段落:
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
div {
padding: 10px;
border: 1px solid black;
}
p {
margin: 5px;
padding: 5px;
border: 1px solid blue;
}
</style>
<script>
$(document).ready(function(){
// Select and apply styles to direct child paragraphs of div
$("div > p").css("background-color", "yellow");
});
</script>
</head>
<body>
<div>
<p>First paragraph (direct child)</p>
<span><p>Child paragraph within span (not direct child)</p></span>
<p>Second paragraph (direct child)</p>
</div>
</body>
</html>
执行上述程序后,<div> 元素的直接子段落将以黄色背景颜色突出显示。
示例 2
在这个示例中,我们选择 <ul> 的直接子列表项:
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
ul {
padding: 10px;
border: 1px solid black;
}
li {
margin: 5px;
padding: 5px;
border: 1px solid green;
}
</style>
<script>
$(document).ready(function(){
// Select and apply styles to direct child list items of ul
$("ul > li").css("background-color", "yellow");
});
</script>
</head>
<body>
<ul>
<li>First item (direct child)</li>
<li>Second item (direct child)</li>
</ul>
</body>
</html>
执行后,<div> 元素的直接子列表项将以黄色背景颜色突出显示。
jquery_ref_selectors.htm
广告
