jQuery | parent()&parents()及其示例

这个 父项() 是jQuery中的一个内置方法,用于查找与所选元素相关的父元素。jQuery中的这个parent()方法向上遍历所选元素并返回该元素。 语法:

null
$(selector).parent()

这里的选择器是父元素需要查找的选定元素。 参数: 它不接受任何参数。 返回值: 它返回选定元素的父元素。 显示此函数工作的jQuery代码:

< html >
< head >
< style >
.main_div * {
display: block;
border: 1px solid green;
color: green;
padding: 5px;
margin: 15px;
}
</ style >
< script
</ script >
< script >
$(document).ready(function() {
$("span").parent().css({
"color": "green",
"border": "2px solid green"
});
});
</ script >
</ head >
< body >
< div class = "main_div" >
< div style = "width:500px;" >div (Great-Grandparent)
< ul >This is the grand-parent of the selected span element.!
< li >This is the parent of the selected span element.!
< span >This is the span element !!!</ span >
</ li >
</ ul >
</ div >
</ div >
</ body >
</ html >


在上面的代码中,只有所选元素的父元素是get deep green Color。 输出: 图片[1]-jQuery | parent()&parents()及其示例-yiteyi-C++库

这个 父母() 是jQuery中的一个内置方法,用于查找与所选元素相关的所有父元素。jQuery中的parents()方法遍历所选元素的所有级别,并返回所有元素。 语法:

$(selector).parents()

这里的选择器是所有父元素都需要找到的选定元素。 参数: 它不接受任何参数。 返回值: 它返回所选元素的所有父元素。 显示此函数工作的jQuery代码:

< html >
< head >
< style >
.main_body* {
display: block;
border: 2px solid green;
color: green;
padding: 5px;
margin: 15px;
}
</ style >
< script
</ script >
< script >
$(document).ready(function() {
$("span").parents().css({
"color": "green",
"border": "2px solid green"
});
});
</ script >
</ head >
< body class = "main_body" >
< div style = "width:500px;" >This is the great grand parent of the selected span element.!
< ul >This is the grand parent of the selected span element.!
< li >This is the parent of the selected element.!
< span >This is the selected span element.!</ span >
</ li >
</ ul >
</ div >
</ body >
</ html >


在上面的代码中,所选元素的所有父元素都以深绿色显示。 输出: 图片[2]-jQuery | parent()&parents()及其示例-yiteyi-C++库

jQuery是一个开源JavaScript库,可以简化HTML/CSS文档之间的交互,它以其独特的编程理念而广为人知 “少写多做” . 通过以下步骤,您可以从头开始学习jQuery jQuery教程 jQuery示例 .

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享