首页 > Web开发 > 详细

jquery中子元素和后代元素的区别

时间:2016-09-27 20:22:32      阅读:278      评论:0      收藏:0      [点我收藏+]

本文转载,原网站地址:http://blog.sina.com.cn/s/blog_775da6710100vzbr.html

 

对于刚学jquery的朋友来说,有时候会对jquery的层选择器$("ancestor descendant")和$("parent>child")产生一点理解上的问题。其实很简单,看完下面的例子你就会完完全全明白了。

 

<div>This is <strong>very</strong> important.</div>

<div>This is <em>really <strong>very</strong></em> important.</div>

 

以上是html。运行后在浏览器里是看到

 

This is very important.
This is really very important.
 

这样的样式,为了容易看到效果,我们不妨就尝试用添加css颜色来试一下

 

如果运行$("div strong").css("color","red");就是把div的后代元素strong变为红色。运行后是

 

This is very important.
This is really very important.
 
 如果运行$("div>strong").css("color","blue");就是把div的子元素strong变为蓝色。运行后是
 
This is very important.
This is really very important.
 

看到这里我想应该都明白了吧,如果再不懂的话,可以通俗的这样理解,例如有一家人三代同堂,爷爷,父亲和你,这样话爷爷的子元素就是你父亲,同时你父亲也是爷爷的后代元素,而你只是爷爷的后代元素。

jquery中子元素和后代元素的区别

原文:http://www.cnblogs.com/lixiang-01/p/5914007.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!