jQuery选择器之子元素选择器详解
更新时间:2017年09月18日 13:50:49 作者:喵嘻嘻
这篇文章主要为大家详细介绍了jQuery选择器之子元素选择器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了jQuery子元素选择器,供大家参考,具体内容如下
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title></title> <link rel="stylesheet" href="imooc.css" rel="external nofollow" type="text/css"> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> </head> <body> <h2>子元素筛选选择器</h2> <h3>:first-child、:last-child、:only-child</h3> <div class="left first-div"> <div class="div"> <a>:first-child</a> <a>第二个元素</a> <a>:last-child</a> </div> <div class="div"> <a>:first-child</a> </div> <div class="div"> <a>:first-child</a> <a>第二个元素</a> <a>:last-child</a> </div> </div> <script type="text/javascript"> //查找class="first-div"下的第一个a元素 //针对所有父级下的第一个 $(".first-div a:first-child").css("color", "#CD00CD"); </script> <script type="text/javascript"> //查找class="first-div"下的最后一个a元素 //针对所有父级下的最后一个 //如果只有一个元素的话,last也是第一个元素 $(".first-div a:last-child").css("color", "red"); </script> <script type="text/javascript"> //查找class="first-div"下的只有一个子元素的a元素 $(".first-div a:only-child").css("color", "blue"); </script> <h3>:nth-child、:nth-last-child</h3> <div class="left last-div"> <div class="div"> <a>:first-child</a> <a>第二个元素</a> <a>第三个元素</a> <a>:last-child</a> </div> <div class="div"> <a>:first-child</a> <a>第二个元素</a> </div> <div class="div"> <a>:first-child</a> <a>第二个元素</a> <a>第三个元素</a> <a>:last-child</a> </div> </div> <script type="text/javascript"> //查找class="last-div"下的第二个a元素 $(".last-div a:nth-child(2)").css("color", "#CD00CD"); </script> <script type="text/javascript"> //查找class="last-div"下的倒数第二个a元素 $(".last-div a:nth-last-child(2)").css("color", "red"); </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
使用jQuery快速解决input中placeholder值在ie中无法支持的问题
本篇文章主要介绍了使用jQuery快速解决input中placeholder值在ie中无法支持的问题。需要的朋友可以过来参考下,希望对大家有所帮助2014-01-01如何让浏览器支持jquery ajax load 前进、后退功能
在做 ajax load 的时候,很多人都不会考虑到需要浏览器支持前进后退功能,本例让浏览器支持jquery ajax load 前进、后退 功能,需要的朋友可以参考下2014-06-06
最新评论