siblings() Get the siblings of each element in the matching set. Filtering by selector is optional.
jQuery’s traversal method siblings()
$("给定元素").siblings(".selected")
Its function is to filter the given sibling elements (excluding the given element itself)
Example: Web page options bar
When you click on any tab, the other two tabs will change their styles and their contents will be hidden.
The following is the html code.
<body> <ul id="menu"> <li class="tabFocus">家居</li> <li>电器</li> <li>二手</li> </ul> <ul id="content"> <li class="conFocus">我是家居的内容</li> <li>欢迎您来到电器城</li> <li>二手市场,产品丰富多彩</li> </ul> </body>
jQuery code
<script type="text/javascript"> $(function() { $("#menu li").each(function(index) { //带参数遍历各个选项卡 $(this).click(function() { //注册每个选卡的单击事件 $("#menu li.tabFocus").removeClass("tabFocus"); //移除已选中的样式 $(this).addClass("tabFocus"); //增加当前选中项的样式 //显示选项卡对应的内容并隐藏未被选中的内容 $("#content li:eq(" + index + ")").show() .siblings().hide();//#menu与#content在html层没有嵌套关联,但因为其ul序列相同,用index值可以巧妙的将两者关联。 }); }); }) </script>
Which
When the corresponding tab is clicked, the content of the clicked tab will be show(). The other two sibling elements
In this way, the content of the clicked area can be dynamically displayed, while the other two tabs can be hidden.
and
$("给定元素").siblings(".selected")
means to filter the sibling elements of the given element class named .selected (excluding the given element itself)
The above content is a detailed explanation of jQuery siblings() usage examples. I hope it will be helpful to everyone!