Files
notes_estom/JavaScript/jQuery/5遍历.md
2020-07-21 09:13:01 +08:00

1.2 KiB
Raw Blame History

1 定义

与Xpath选择器十分详细。在简介中通过css选择器能够锁定目标元素。

2 向上遍历

  • parent()
  • parents()
  • parentsUntil()
$(document).ready(function(){
  $("span").parents("ul");
});

$(document).ready(function(){
  $("span").parentsUntil("div");
});

3 向下遍历

  • children()
  • find()
$(document).ready(function(){
  $("div").children();
});

$(document).ready(function(){
  $("div").find("span");
});

4 水平遍历

  • siblings()
  • next()
  • nextAll()
  • nextUntil()
  • prev()
  • prevAll()
  • prevUntil()
$(document).ready(function(){
  $("h2").siblings();
});

$(document).ready(function(){
  $("h2").next();
});

$(document).ready(function(){
  $("h2").nextUntil("h6");
});

5 遍历过滤

  • first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素。
  • filter() 和 not() 允许您选取匹配或不匹配某项指定标准的元素。
$(document).ready(function(){
  $("div p").first();
});
$(document).ready(function(){
  $("div p").last();
});
$(document).ready(function(){
  $("p").eq(1);
});
$(document).ready(function(){
  $("p").filter(".url");
});
$(document).ready(function(){
  $("p").not(".url");
});