分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

jQuery选择器之子元素选择器

发布时间:2023-09-06 01:12责任编辑:彭小芳关键词:jQuery选择器
<!DOCTYPE html><html><head> ???<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> ???<title></title> ???<link rel="stylesheet" href="imooc.css" 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实现:nth-child(n)是严格来自CSS规范,所以n值是“索引”,也就是说,从1开始计数,:nth-child(index)从1开始的,而eq(index)是从0开始的

jQuery选择器之子元素选择器

原文地址:http://www.cnblogs.com/sunxirui00/p/7542568.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved