JQuery学习总结之 -- siblings()

siblings(): 获得匹配集合中每个元素的同胞

1、span的同胞元素中类名为selected的元素设为背景黄色Hello Again

<body>
  <div><span>Hello</span></div>
  <p class="selected">Hello Again</p>
  <span>And Again</span>

<script>
$("span").siblings(".selected").css("background", "yellow");
</script>

</body>

2、类名为third-item的li标签的同胞元素设置背景红色,即list item 1245为红色背景

<body>
<ul>
   <li>list item 1</li>
   <li>list item 2</li>
   <li class="third-item">list item 3</li>
   <li>list item 4</li>
   <li>list item 5</li>
</ul>

<script>
$('li.third-item').siblings().css('background-color', 'red');
</script>

</body>


你可能感兴趣的:(JQuery学习总结之 -- siblings())