以下是使用 jQuery 完成选取 id 为 two(div 元素) 的所有兄弟元素(前后) 的方法:

  1. siblings 方法:
$("#two").siblings();
  1. prevAll 方法:
$("#two").prevAll();
  1. nextAll 方法:
$("#two").nextAll();
  1. prevUntil 方法:
$("#two").prevUntil();
  1. nextUntil 方法:
$("#two").nextUntil();
  1. siblings、prevAll、nextAll、prevUntil 和 nextUntil 方法可以接受一个选择器作为参数,以进一步过滤匹配的兄弟元素。例如,如果只想选择 class 为 "myClass" 的兄弟元素,可以这样写:
$("#two").siblings(".myClass");
$("#two").prevAll(".myClass");
$("#two").nextAll(".myClass");
$("#two").prevUntil(".myClass");
$("#two").nextUntil(".myClass");

原文地址: https://www.cveoy.top/t/topic/pKZp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录