简体   繁体   English

如何在锚标签上正确使用兄弟姐妹?

[英]How correct use siblings on anchor tag?

 $("a").click(function() { $(this).siblings().hide() })
 body { display: flex; justify-content: center; margin-top: 300px; } a { font-size: 30px; }
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <ul> <li><a>one</a></li><br><br> <li><a>two</a></li><br><br> <li><a>three</a></li><br><br> </ul>

i click anyone anchor other brother element not hide.我点击任何人锚其他兄弟元素不隐藏。

but change make $("a") to $("li") is work.但是将 make $("a") 更改为 $("li") 是可行的。 why?为什么?

The element a has no sibling, its the only child of li .元素a没有兄弟,它是li的唯一子元素。

In order to hide the other li s, you should use sibling on them:为了隐藏其他li s,你应该对它们使用 sibling :

 $("a").click(function() { $(this).parents("li").siblings().hide() })
 body { display: flex; justify-content: center; } a { font-size: 30px; }
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <ul> <li><a>one</a></li><br><br> <li><a>two</a></li><br><br> <li><a>three</a></li><br><br> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM