.parent()

作用:选中指定元素的直接父元素

【例】点击按钮,改变其父元素的背景颜色,效果如下

代码

<html lang="en">
<head><style>div {width: 100px;height: 50px;text-align: center;background-color: steelblue;position: absolute;}div.demo {left: 113px;top: 8px;}.active {background-color: teal;}</style>
</head>
<body><div><span>span-1</span><button>点一下</button></div><div class="demo"><span>span-2</span><button>点一下</button></div>
</body>
<script src="./jquery.js"></script>
<script>$('button').click(function () {if ($(this).parent().hasClass('active')) {$(this).parent().removeClass('active');} else {$(this).parent().addClass('active');}})
</script>
</html>

.parents()

作用:获取指定元素的所有父元素

【例】点击按钮,给按钮的第二层的父元素添加样式,效果如下

代码

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.wrapper {width: 250px;height: 100px;color: #fff;background-color: #fff;margin-bottom: 5px;position: relative;border-radius: 15px;}.wrapper .demo {margin: 0 auto;width: 200px;height: 50px;position: absolute;left: 50%;top: 50%;margin-left: -100px;margin-top: -25px;text-align: center;}.wrapper .demo span {display: block;margin-bottom: 3px;}.wrapper button {border: none;outline: none;background-color: #eee;border-radius: 3px;color: #424242;cursor: pointer;}.active {background-color: #424242;}</style>
</head>
<body><div class="wrapper"><div class="demo"><span>黑夜之中,才见繁星</span><button>点击</button></div></div><div class="wrapper"><div class="demo"><span>风过云散,星光遍地</span><button>点击</button></div></div>
</body>
<script src="./jquery.js"></script>
<script>$('button').click(function () {$(this).parents('.wrapper').addClass('active');});console.log($('button').parents());
</script>
</html>

打印结果如下

由打印结果可知,该方法可选择他所有的父级元素节点,包括body和html,且共同的父级只选中一次

.offsetParent()

作用:选择最近的有定位的父级,若没有定位的父级,则选中html

【例】点击一次按钮,给对应的父级添加定位,然后用offsetParent方法寻找有定位的父级,给其添加背景颜色,例子举得有点牵强,主要是为了练习jQuery的链式调用和一些方法,代码效果如下

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}button {width: 50px;height: 50px;}.box {width: 50px;height: 50px;padding: 50px;background-color: black;}.content {width: 150px;height: 150px;padding: 50px;background-color: black;}.wrapper {width: 250px;height: 250px;padding: 50px;background-color: black;margin: 20px;}.active {position: relative;}.color {background-color: cadetblue;}</style>
</head>
<body><div class="wrapper"><div class="content"><div class="box"><button></button></div></div></div>
</body>
<script src="./jquery.js"></script>
<script>var i = 0;$('button').click(function () {var len = $('button').parents('div').size();if (i>len-1) {  //注i=0;}$('button').parents('div').removeClass('active').eq(i++).addClass('active').end().removeClass('color').end().offsetParent().addClass('color');})
</script>
</html>

【注】两个注意点:offsetParent方法不接受任何参数;代码中若if条件设置i>len,则当经过3次点击后,i=3,值不大于3,则执行最后60行代码时,i=3,而由于在parents方法中只选择了是div的父级给其添加定位,因此,此时没有有定位的父级,故offsetParent直接选中html去添加背景颜色,具体效果如下

可以看到第四次点击的时候选中了文档,且再点击没有移除文档的背景颜色,还是因为parents方法没有选中html的原因

.closets()

作用:若参数选中ul,则该方法选择离指定元素最近的是ul的父级

【例】选择离按钮最近的ul父级,改变其背景颜色,代码效果

代码

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;border-radius: 8px;}.wrapper {margin: 20px;width: 200px;height: 200px;background-color: cadetblue;}ul {list-style: none;}.content>li {width: 100%;height: 90px;margin-bottom: 20px;}.content>li .box {width: 200px;height: 40px;margin-bottom: 10px;}.content>li .box li {width: 100%;height: 100%;}button {width: 40px;height: 40px;line-height: 40px;border: none;outline: none;cursor: pointer;}.active {background-color: darkseagreen;}</style>
</head>
<body><div class="wrapper"><ul class="content"><li><ul class="box"><li><button>btn1</button></li></ul><ul class="box"><li><button>btn3</button></li></ul></li><li><button>btn4</button></li></ul></div>
</body>
<script src="./jquery.js"></script>
<script>$('button').click(function () {$('ul').removeClass('active');$(this).closest('ul').addClass('active');})
</script>
</html>

jQuery——parent(),parents(),offsetParent(),closets()方法相关推荐

  1. jquery学习之-查找父元素方法parent() parents() closest()的区别

    parent().parents()与closest()方法两两之间有类似又有不同,本篇简短的区分一下这三个方法.通过本篇内容,大家将会在以后使用.parent().parents()和closest ...

  2. jQuery学习(十二)—jQuery中对象的查找方法总结

    jQuery学习(十二)-jQuery中对象的查找方法总结 一.find方法 作用:在元素1中查找元素2,类似于选择器中的后代选择器 格式:元素1.find(元素2),元素2为CSS选择器或者jQue ...

  3. jquery 常见选择器以及一些方法

    // 这里的selector表示具体的选择器 jQuery( "selector:first" ) jQuery的:first选择器用于获取匹配到的第一个元素,将其封装为jQuer ...

  4. jQuery代码优化的9种方法

    前面的话 本文将详细介绍jQuery代码优化的9种方法 用对选择器 在jQuery中,可以用多种选择器,选择同一个网页元素.每种选择器的性能是不一样的,应该了解它们的性能差异 1.最快的选择器:id选 ...

  5. Jquery下的Ajax调试方法

    Jquery下的Ajax调试方法 介绍 本文介绍Jquery下的Ajax调试方法:很多调试方法,就是一点就通,但是,在还没有通之前,会让人困惑,不知所以然: Ajax 可以为用户提供更为丰富的用户体验 ...

  6. 冻结html表格标题列,jQuery实现冻结表头的方法

    本文实例讲述了jQuery实现冻结表头的方法.分享给大家供大家参考.具体如下: 前段时间做项目时候由于需要显示一个列表,但是由于数据太多在滚动的时候表头必须冻结住,所以就写了下面这个脚本(曾经在网上也 ...

  7. 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变

    查看本章节 查看作业目录 需求说明: 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变 当用户单击"+" ...

  8. Jquery中find与each方法使用详解

    本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...

  9. Jquery Ajax调用aspx页面方法

    原文:Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通 ...

最新文章

  1. Deseq2的理论基础
  2. SecureCRT提示----数据库里没找到防火墙“无”----解决方案
  3. GraphSage模型cora数据集
  4. 机器学习——决策树的实现
  5. java实现18位校验
  6. 毕啸南专栏 | 对话今日头条副总裁马维英:有技术也要有价值观
  7. python 小知识总结汇整
  8. FPGA实现FIR滤波器
  9. bat快捷方式启动局域网共享文件
  10. 华硕主板固态硬盘不识别_主板启动设置无法识别固态硬盘 - 卡饭网
  11. 从中序与后序遍历序列构造二叉树
  12. w3c html验证服务,W3C验证和Vue的HTML绑定语法(W3C Validation and Vue's HTML binding syntax)...
  13. 敏捷开发的PRD该怎么写
  14. C# WPF MVVM 实战 – 2.2
  15. jsp异常 The JSP specification requires that an attribute name is preceded by whitespace
  16. android平板电脑卡槽在哪,外观|增加SIM卡槽_酷比魔方 IWORK8_平板电脑评测-中关村在线...
  17. derived(derived什么意思)
  18. Digital Roots
  19. chrome 火狐_添加有趣的图形以记住Firefox或Chrome中的Milk标志
  20. Typora 设置代码块的默认编程语言

热门文章

  1. 第43讲:灵活好用的 Spider 的用法
  2. python之多线程编程(一):基本介绍
  3. 【送书福利8本】YYDS《剑指Offer》,百万程序员人手一册
  4. CI/CD大幅减少甩锅!
  5. 可算是有文章,把Linux零拷贝讲透彻了!
  6. 技巧:Go 结构体如何转换成 map[string]interface{}
  7. 美摄 - 助力打造完善的音视频解决方案
  8. CDN调试—Debug Headers
  9. 腾讯开源 TurboTransformers:自然语言处理推理加速工具
  10. 大牛书单 | 人工智能方向好书推荐