Jquery的parent和parents(找到某一特定的祖先元素)用法

news/2024/7/4 1:44:29 标签: jquery, div, function, input, table, class
class="baidu_pl"> <class="tags" href="/tags/DIV.html" title=div>div id="article_content" class="article_content clearfix"> <class="tags" href="/tags/DIV.html" title=div>div id="content_views" class="htmledit_views">
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);


class="tags" href="/tags/DIV.html" title=div>div> class="tags" href="/tags/DIV.html" title=div>div> <class="tags" href="/tags/DIV.html" title=div>div id="treeSkill">class="tags" href="/tags/DIV.html" title=div>div>

http://www.niftyadmin.cn/n/1732237.html

相关文章

《地图气球》小程序从产品到运维的个人全栈开发过程分享(长文)

前言 怕过不了审&#xff0c;先声明一下&#xff0c;这不是广告&#xff0c;因为这个小程序没上架。 从5年前入行的时候就一直想做一个社交产品&#xff0c;最近工作略闲&#xff0c;加之小程序火爆&#xff0c;下班后时间多&#xff0c;于是就花费了一个月业余时间&#xff…

jquery 里的each使用方法

each()方法能使DOM循环结构简洁&#xff0c;不容易出错。each()函数封装了十分强大的遍历功能&#xff0c;使用也很方便&#xff0c;它可以遍历一维数组、多维数组、DOM, JSON 等等 在javaScript开发过程中使用$each可以大大的减轻我们的工作量。 下面提一下each的几种常用的…

传智播客JQuery实战(四):标签页效果

第一个标签页中鼠标滑过显示不同的标签页&#xff0c;第二个标签页中点击不同标签加载其他页面中的内容&#xff0c;加载等待的图片缓慢隐藏&#xff1a; /WebRoot/4.Tab.html&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &quo…

《从零构建前后分离web项目》:开篇 - 纵观WEB历史演变

开篇 &#xff1a; 纵观WEB历史演变 在校学习和几年工作工作中不知不觉经历了一半的 WEB 历史演变、对近几年的发展比较了解&#xff0c;结合经验聊聊 WEB 发展历史。 演变不易&#xff0c;但也是必然&#xff0c;因为为人始终要进步。 WEB 的发展史 一、开山鼻祖 - 石器时代…

腾讯校招前端开发笔试初试总结| 掘金技术征文

作者&#xff1a;Chacha 今天来总结下9月5号做的前端笔试题和9月16号的腾讯初试&#xff0c;这段时间真的没怎么准备面试和笔试&#xff0c;可想而知&#xff0c;腾讯的面试肯定是挂了。 首先是9月5号的腾讯校招笔试题&#xff0c;在做笔试之前&#xff0c;我都没怎么去刷题&a…

理解异步之美:Promise与async await(一)

你可能会放出一个怪物 异步与同步相比&#xff0c;最难以掌控的就是异步的任务会什么时候完成和完成之后的回调问题&#xff0c; 难以掌控的触发状态&#xff0c;让你自己写的代码当时还可以读懂&#xff0c;但是过几天、半个月之后如果不重新盘一边逻辑&#xff0c;你哪知道…

vue中前进刷新、后退缓存用户浏览数据和浏览位置的实践

vue中&#xff0c;我们所要实现的一个场景就是&#xff1a; 1.搜索页面>到搜索结果页时&#xff0c;搜索结果页面要重新获取数据&#xff0c; 2.搜索结果页面>点击进入详情页>从详情页返回列表页时&#xff0c;要保存上次已经加载的数据和自动还原上次的浏览位置。 最…

【转】JQuery实现页面随滚动条滚动而动态加载内容的效果

新浪微博有这个功能,刚才思考一下 ,简单的写了一下实现方法,代码是Js的.无可否认,这种方式应该算是web2.0的产物,在用户体验上具备很好的感受,除了微博在使用该方式外,另外我还发现有几个其他性质的网站也在用这种方式,原因是他们的网站页面比较长,所以用户在浏览的时候随着滚动…