jQuery查找——parent/parents/parentsUntil/closest

前端开发 作者: 2024-08-26 09:05:01
jquery的parent(),parents(),parentsUntil(),closest()都是向上查找父级元素,具体用法不同 parent():取得一个包含着所有匹配元素的唯一父元素的元素集
 1 <!DOCTYPE html>
 2 <html 3     head 4         meta charset="utf-8" 5         title></ 6         script type="text/javascript" src="js/jquery-1.7.2.js" script 7          8             $(function(){
 9                 //jquery
10             });
11              changeColor(a){
12                 //changeColor
13             }
14         </15     16     body17         ul class="level-1 yes"18             li ="item-i">Ili19             ="item-ii">II
20                 ="level-2 yes"21                     ="item-a">A22                     ="item-b"B
23                         ="level-3"24                             ="item-1">125                             ="item-2">226                             ="item-3">327                             ><a href="javascript:;" onclick="changeColor(this);">改变颜色a28                         ul29                     30                     ="item-c">C31                 32             33             ="item-iii">III34         35     36 >
$("li.item-1").parent().css("border","3px solid blue");
$("li.item-1").parent(".level-3").css("border","3px solid blue");
$("li.item-1").parent(".level-1").css("border","3px solid blue");
$("li").parent().css("border","3px solid blue");
$("li").parent(".level-2").css("border","3px solid blue");
$("a").parent().parent().parent().css("border","3px solid red");
function changeColor(a){
    $(a).parent().parent().parent().css("border","3px solid red");
}
$("li").parent("ul").css("border","3px solid blue");
$("li").parent("ul").css("border","3px solid blue");
var rets = $("li").parent("ul");
rets[0].css("border","3px solid red");
$("li.item-1").parents().css("border","3px solid blue");
$("li.item-1").parents(".yes").css("border","3px solid blue");
function changeColor(a){
    $(a).parents(".item-b").css("border","3px solid red");
}
$("li.item-1").parents("ul").css("border","3px solid blue");
$("li.item-1").parents("ul").css("border","3px solid blue");
var rets = $("li.item-1").parents("ul");
rets[0].css("border","3px solid red");
$("li.item-1").parentsUntil().css("border","3px solid blue");
$("li.item-1").parentsUntil(".level-2").css("border","3px solid blue");
$("li.item-1").parentsUntil(".level-2",".level-3").css("border","3px solid blue");
function changeColor(a){
     $(a).parentsUntil(".item-b").css("border","3px solid red");
}
$("li.item-1").parentsUntil("ul.level-1").css("border","3px solid blue");
$("li.item-1").parentsUntil("ul.level-1").css("border","3px solid blue");
var parents = $("li.item-1").parentsUntil("ul.level-1");
parents[0].css("border","3px solid red");
$("li.item-1").closest().css("border","3px solid blue");
$("li.item-1").closest("ul.level-2").css("border","3px solid blue");
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68888.html