parent([selector])方法获取元素的直接父级。如果在一组元素上调用,parent返回一组唯一的直接父元素。
selector.parent( [selector] )
这是此方法使用的所有参数的描述-
链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/traversal-parent.html
来源:LearnFk无涯教程网
selector - 这是用于过滤父项的可选选择器。
以下是一个简单的示例,简单说明了此方法的用法。
<html> <head> <title>The jQuery Example</title> <script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type = "text/javascript" language = "javascript"> $(document).ready(function(){ $("p").parent().addClass('hilight'); }); </script> <style> .hilight { background:yellow; } </style> </head> <body> <scan>Top Element</scan> <div> <div>sibling<div>child</div></div> <p>sibling</p> <scan>sibling</scan> </div> </body> </html>
这将产生以下输出-
Top Element siblingchildsibling
sibling
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)