选择同一级别的其他项目

Ozk*_*kan -2 html javascript jquery

考虑下面的html

<ul id="headmenu">
    <li id="headmenuitem1"></li>
    <li id="headmenuitem2"></li>
    <li id="headmenuitem3"></li>
    <li id="headmenuitem4"></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我需要在一个项目上选择所有其他元素.比如我在徘徊headmenuitem1

 $(document).ready(function () {
     $("#headmenuitem1").hover(function () { //When item is hovered...
        $(this).... //(select all the other items)
     })
 });
Run Code Online (Sandbox Code Playgroud)

在做的时候this我需要选择所有其他项目,因此在示例中,它必须选择项目headmenuitem2, headmenuitem3 and headmenuitem4

有什么建议?谢谢

dku*_*mar 13

您有以下选择:

  1. 所有级别除外this:

    $(this).siblings(); 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 所有级别包括this,用于.not(this)排除this:

    $(this).parent().children();
    
    Run Code Online (Sandbox Code Playgroud)
  3. 以前在同一水平上:

    $(this).prev();
    
    Run Code Online (Sandbox Code Playgroud)
  4. 紧接着在同一级别:

    $(this).next();
    
    Run Code Online (Sandbox Code Playgroud)
  5. 所有以前在同一水平:

    $(this).prevAll();
    
    Run Code Online (Sandbox Code Playgroud)
  6. 所有下一个在同一级别:

    $(this).nextAll();
    
    Run Code Online (Sandbox Code Playgroud)

希望你能通过名字本身理解语法......