jQuery.parentsUntil()方法
jQuery.parentsUntil()方法可以用来查找祖先元素,它可以指定一个终止元素,在此终止元素之前的所有祖先元素都会被查找出来。
$("#child").parentsUntil("#parent"); // 查找#child元素的所有祖先元素,直到#parent元素为止
jQuery.parentsUntil()方法返回一个jQuery对象,可以通过这个对象来操作找到的元素,比如:
$("#child").parentsUntil("#parent").css("background-color","red"); // 把找到的元素的背景色设置为红色
jQuery.parentsUntil()方法还可以指定一个过滤器,只有满足过滤器要求的元素才会被查找出来,比如:
$("#child").parentsUntil("#parent",".className"); // 查找#child元素的所有class为className的祖先元素,直到#parent元素为止
jQuery.parentsUntil()方法还可以指定一个函数,只有满足函数要求的元素才会被查找出来,比如:
$("#child").parentsUntil("#parent",function(index){ return $(this).attr("id") === "idName"; // 查找#child元素的所有id为idName的祖先元素,直到#parent元素为止 });
jQuery.parentsUntil()方法可以用来查找祖先元素,可以指定一个终止元素,也可以指定一个过滤器或一个函数,来筛选出满足要求的祖先元素。