jQuery选择器之子元素选择器详解


Posted in jQuery onSeptember 18, 2017

本文实例为大家分享了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 相关文章推荐
jQuery实现百度登录框的动态切换效果
Apr 21 jQuery
jQuery实现按比例缩放图片的方法
Apr 29 jQuery
jQuery上传插件webupload使用方法
Aug 01 jQuery
jquery实现企业定位式导航效果
Jan 01 jQuery
jQuery实现定时隐藏对话框的方法分析
Feb 12 jQuery
jquery动态添加带有样式的HTML标签元素方法
Feb 24 jQuery
jQuery实现的简单获取索引功能示例
Jun 04 jQuery
jQuery实现鼠标移入移出事件切换功能示例
Sep 06 jQuery
解决JQuery的ajax函数执行失败alert函数弹框一闪而过问题
Apr 10 jQuery
jQuery操作cookie的示例代码
Jun 05 jQuery
jquery树形插件zTree高级使用详解
Aug 16 jQuery
jQuery实现购物车全功能
Jan 11 jQuery
jQuery实现的表格前端排序功能示例
Sep 18 #jQuery
jQuery实现对网页节点的增删改查功能示例
Sep 18 #jQuery
jQuery UI 实例讲解 - 日期选择器(Datepicker)
Sep 18 #jQuery
jQuery实现火车票买票城市选择切换功能
Sep 15 #jQuery
jQuery获取复选框选中的当前行的某个字段的值
Sep 15 #jQuery
jQuery plugin animsition使用小结
Sep 14 #jQuery
jquery 键盘事件的使用方法详解
Sep 13 #jQuery
You might like
php a simple smtp class
2007/11/26 PHP
discuz的php防止sql注入函数
2011/01/17 PHP
php数组函数序列之in_array() - 查找数组中是否存在指定值
2011/11/07 PHP
php中使用parse_url()对网址进行解析的实现代码(parse_url详解)
2012/01/03 PHP
php中字符串和整数比较的操作方法
2019/06/06 PHP
laravel 如何实现引入自己的函数或类库
2019/10/15 PHP
网上抓的一个特效
2007/05/11 Javascript
Javascript this关键字使用分析
2008/10/21 Javascript
js select option对象小结
2013/12/20 Javascript
js中 javascript:void(0) 用法详解
2015/08/11 Javascript
jQuery控制div实现随滚动条滚动效果
2016/06/07 Javascript
js插件Jcrop自定义截取图片功能
2016/10/14 Javascript
详解vuejs之v-for列表渲染
2017/06/22 Javascript
JS实现图片转换成base64的各种应用场景实例分析
2018/06/22 Javascript
你不知道的Vue技巧之--开发一个可以通过方法调用的组件(推荐)
2019/04/15 Javascript
基于Node的Axure文件在线预览的实现代码
2019/08/28 Javascript
vue实现数字滚动效果
2020/06/29 Javascript
Python调用C++程序的方法详解
2017/01/24 Python
python实现Adapter模式实例代码
2018/02/09 Python
解决Python中list里的中文输出到html模板里的问题
2018/12/17 Python
python threading和multiprocessing模块基本用法实例分析
2019/07/25 Python
Python的bit_length函数来二进制的位数方法
2019/08/27 Python
Python 存取npy格式数据实例
2020/07/01 Python
浅析Python迭代器的高级用法
2020/07/16 Python
浅谈three.js中的needsUpdate的应用
2012/11/12 HTML / CSS
HTML5 Canvas像素处理使用接口介绍
2012/12/02 HTML / CSS
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
教师推荐信范文
2013/11/24 职场文书
大学生自我鉴定范文
2013/12/28 职场文书
大学生职业生涯规划范文——找准自我,定位人生
2014/01/23 职场文书
出国留学计划书
2014/04/27 职场文书
终止劳动合同协议书
2014/10/05 职场文书
商务代表岗位职责
2015/02/15 职场文书
倡议书怎么写?
2019/04/11 职场文书
劳务派遣管理制度(样本)
2019/08/23 职场文书
python异常中else的实例用法
2021/06/15 Python