jQuery parent() 方法

jQuery 教程 · 2019-03-28 09:57:14

实例

返回 <span> 的直接父元素:

$(document).ready(function(){
$("span").parent().css({"color":"red","border":"2px solid red"});
});

结果:

body (great-great-grandparent)
div (great-grandparent)
    ul (grandparent)
  • li (direct parent) span

定义和用法

parent() 方法返回被选元素的直接父元素。

DOM 树:该方法只沿着 DOM 树向上遍历单一层级。如需向上遍历直至文档根元素的所有路径(返回祖父节点或其他祖先),请使用 parents() 或 parentsUntil() 方法。

提示:如需沿着 DOM 树向下遍历单一层级,或向下遍历直至最后一个后代的所有路径(返回子节点或其他后代),请使用 children() 或 find() 方法。

语法

$(selector).parent(filter)

参数 描述
filter 可选。规定缩小搜索父元素范围的选择器表达式。

点击查看所有 jQuery 教程 文章: https://www.codercto.com/courses/l/35.html

查看所有标签

Think Python

Think Python

Allen B. Downey / O'Reilly Media / 2012-8-23 / GBP 29.99

Think Python is an introduction to Python programming for students with no programming experience. It starts with the most basic concepts of programming, and is carefully designed to define all terms ......一起来看看 《Think Python》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具