jQuery子选择器(X> Y)用于选择与“ Y”匹配的所有“ X”元素的子元素。
例如,
- $('form> input') –选择与<input>匹配的所有元素,这些元素是与<form>匹配的元素的子元素,只有子元素将匹配,孙子元素,曾孙将不匹配。
jQuery示例
在此示例中,仅“文本框1”和“文本框4”将是匹配的(表单元素的子元素),“文本框2”(孙子)和“文本框3”(大孙子)将不匹配。
<html>
<head>
<title>jQuery child selector example</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<style type="text/css">
div { padding:8px 0px 8px 8px; }
</style>
</head>
<script type="text/javascript">
$(document).ready(function(){
$("form > input").css("border", "2px solid red");
});
</script>
<body>
<h1>jQuery child selector example</h1>
<form>
<label>TextBox 1 (Child) : </label><input name="textbox1">
<div class="level1">
<label>TextBox 2 (GrandChild) : </label><input name="textbox2">
</div>
<div class="level1">
<div class="level2">
<label>TextBox 3 (Great GrandChild) : </label><input name="textbox3">
</div>
</div>
<label>TextBox 4 (Child) : </label><input name="textbox4">
</form>
<div> I'm form siblings #1 - DIV</div>
<p> I'm form siblings #2 - P </p>
<div> I'm form siblings #3 - DIV </div>
<p> I'm form siblings #4 - P </p>
</body>
</html>
翻译自: https://mkyong.com/jquery/jquery-child-selector-example/