class="language-html"><!--
parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合。
parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素)。可以通过一个可选的表达式进行筛选。
parent取得很明确就是当前元素的父元素
parents则是当前元素的祖先元素
-->
<html>
<head></head>
<body>
<class="tags" href="/tags/DIV.html" title=div>div id="class="tags" href="/tags/DIV.html" title=div>div1">
<class="tags" href="/tags/DIV.html" title=div>div id="class="tags" href="/tags/DIV.html" title=div>div2"><p></p></class="tags" href="/tags/DIV.html" title=div>div>
<class="tags" href="/tags/DIV.html" title=div>div id="class="tags" href="/tags/DIV.html" title=div>div3" class="a"><p></p></class="tags" href="/tags/DIV.html" title=div>div>
<class="tags" href="/tags/DIV.html" title=div>div id="class="tags" href="/tags/DIV.html" title=div>div4"><p></p></class="tags" href="/tags/DIV.html" title=div>div>
</class="tags" href="/tags/DIV.html" title=div>div>
</body>
<script type="text/javascript" src="class="tags" href="/tags/JQUERY.html" title=jquery>jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$("p").parent(); //取得的是class="tags" href="/tags/DIV.html" title=div>div2、class="tags" href="/tags/DIV.html" title=div>div3、class="tags" href="/tags/DIV.html" title=div>div4
$('p').parent('.a'); //取得是class="tags" href="/tags/DIV.html" title=div>div3
$('p').parent().parent(); //取得是class="tags" href="/tags/DIV.html" title=div>div1(这点比较奇特,不过Jquery对象本身的特点决定了这是可行的)
$('p').parents(); //取得的是class="tags" href="/tags/DIV.html" title=div>div1、class="tags" href="/tags/DIV.html" title=div>div2、class="tags" href="/tags/DIV.html" title=div>div3、class="tags" href="/tags/DIV.html" title=div>div4
$('p').parents('.a'); //取得的是class="tags" href="/tags/DIV.html" title=div>div3
</script>
</html>
class="language-html"> <body>
<class="tags" href="/tags/TABLE.html" title=table>table>
<tr>
<td><class="tags" href="/tags/INPUT.html" title=input>input id="btn1" class="btn" type="button" value="test"/></td>
<td>some text</td>
</tr>
</class="tags" href="/tags/TABLE.html" title=table>table>
</body>
<script type="text/javascript" src="class="tags" href="/tags/JQUERY.html" title=jquery>jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(class="tags" href="/tags/FUNCTION.html" title=function>function(){
$("#btn1").click(class="tags" href="/tags/FUNCTION.html" title=function>function(){
alert($(this).parent().next().html());
});
});
打印结果:some text
其中:
1、this.parent() 获取的是class="tags" href="/tags/INPUT.html" title=input>input前面的td;
2、this.parent().parent() 获取的是tr;
3、this.parent().parent().parent() 获取的是class="tags" href="/tags/TABLE.html" title=table>table;
4、this.parent().next() 获取的是class="tags" href="/tags/INPUT.html" title=input>input前面的td相邻的另一个td。
另一个例子中:
class="language-html"><class="tags" href="/tags/DIV.html" title=div>div>
<p>Hello</p>
<p>Hello</p>
</class="tags" href="/tags/DIV.html" title=div>div>
$('p').parent() 得到的是:
<class="tags" href="/tags/DIV.html" title=div>div><p>Hello</p><p>Hello</p></class="tags" href="/tags/DIV.html" title=div>div>对象,
因为p标签的父标签是class="tags" href="/tags/DIV.html" title=div>div
有关parents
class="language-html"> <class="tags" href="/tags/DIV.html" title=div>div id="one">
<class="tags" href="/tags/DIV.html" title=div>div id="Two">hello</class="tags" href="/tags/DIV.html" title=div>div>
<class="tags" href="/tags/DIV.html" title=div>div id="Three">
<p><a href="#">tonsh</a></p>
</class="tags" href="/tags/DIV.html" title=div>div>
</class="tags" href="/tags/DIV.html" title=div>div>
$("a").parent() 将会得到父对象<p>
$("a").parents() 得到父对象<p><class="tags" href="/tags/DIV.html" title=div>div.3><class="tags" href="/tags/DIV.html" title=div>div.1>
$("a").parents().filter("class="tags" href="/tags/DIV.html" title=div>div") 将得到<class="tags" href="/tags/DIV.html" title=div>div.3><class="tags" href="/tags/DIV.html" title=div>div.1> 还可以写成$("a").parents("class="tags" href="/tags/DIV.html" title=div>div")。
如果想得到<class="tags" href="/tags/DIV.html" title=div>div.2>对象可以写成这样:$("a").parents("class="tags" href="/tags/DIV.html" title=div>div:eq(0)")。
如果点击<a>链接时弹出<class="tags" href="/tags/DIV.html" title=div>div.2>中的内容该怎么办?
var id=$("a").parents("class="tags" href="/tags/DIV.html" title=div>div:eq(1)").children("class="tags" href="/tags/DIV.html" title=div>div:eq(0)").html();
alert(id);