English 中文(简体)
j格发现指数
原标题:jquery find index number of nested li

How would i go about getting the index position number of one of the nested LI s? I can get the topnav index s but cannot seem the children index number of the subnav(s).

I would like to create the selector dynamically to target the subnav based on the index of the topnav.

<div id="nav_container">
    <ul id="topnav">
        <li><a href="#a">menu item 0</a>
            <ul id="subnav0">
                <li><a href="#">sub menu item</a></li>
                <li><a href="#">sub menu item</a></li>
                <li><a href="#">sub menu item</a></li>
                <li><a href="#">sub menu item</a></li>
                <li><a href="#">sub menu item</a></li>
            </ul>
        </li>
        <li><a href="#a">menu item 1</a>
            <ul id="subnav1">
                <li><a href="#">sub menu item</a></li>
                <li><a href="#">sub menu item</a></li>
                <li><a href="#">sub menu item</a></li>
            </ul>
        </li>
        <li><a href="#">menu item 2</a></li>
        <li><a href="#">menu item 3</a></li>
     </ul>
</div>

My j Query Code

 //Event Handler
 function nav_execute(){
    var _index = $( #topnav > li ).index(this);//Returns index number of topnav item selected
    var _subnav =  #subnav + _index.toString();
    var _selector = _subnav +   > li .toString();//the subnav selector
    var _subindex = $(_selector).index();//gets total num of index s
}

//Binded event
$( #topnav > li ).bind( click , nav_execute);
最佳回答
This code seems to have solved my problem. This checks the parent  id  and then performs a simple conditional statement to determine which nav was clicked. It then stores the index(s) to their master vars.


 function nav_execute(e){

    var id = $(this).parent().attr("id").toString();

    if(id == "topnav"){
        _index = $(this).index();
        _subindex = null;
    }else{
        _index = $(this).parent().parent().index();
        _subindex = $(this).index();    
    }

    console.log( _index:   + _index);
    console.log( _subindex:   + _subindex);

    return false;   
}


$( #topnav > li ).live( click , nav_execute);


Thanks for the help from everyone!
问题回答

我确信我确实不理解你的目标,试图用以下方式取代你的 j:

$( #topnav  li ).bind( click , function (){
  alert($(this).index());   
});

See the full example here (this isn t a solution. It is a way to see which elements are firing events and in which order).

因此,I THINK你试图说,我想要拿到“N'sul”中的任何东西被点击?

如果是你试图做的事,那么这项工作就应当发挥作用。

//Event Handler
 function nav_execute(){
    var $subNav = $(this).find("ul");

    //I m not sure what you are trying to do here so im not going to change the functionality
    var subindex = $subNav.index();//gets total num of index s
}

//Binded event
$( #topnav > li ).bind( click , nav_execute);

http://www.un.org。

Per your comment here is a jsfiddle of what you want: http://jsfiddle.net/wuFB7/4/

你们只能为那些被nes住的人创造选择权。 然而,由于你说,它们是动态生成的,因此我将“.bind”改为“.live”,因为这将使事件处理者能够把最初的页码负荷后产生的内容放在一起。





相关问题
Listen for Events from Browser "Find" Window in JavaScript

Is there a way to listen for typing into the browser s "find" window in JavaScript? (source: apple.com) I d like to be able to reinterpret the search text from JavaScript. What do I need to ...

Equivalent of PreviewKeyDown of C# needed for native C++/MFC

I used a ActiveXControl in a C# Forms application and had the possibility to implement a PreviewKeyDown event handler for that ActiveXControl. That was needed to specify that, e.g. the [ALT] key, is ...

Refresh the UI in gwt

I have created some custom composite widget, which listens to an events (in my case loginEvent). Once the event is caught the state of the widget changes so as the way it should look (in my case I ...

How to remove "onclick" with JQuery?

PHP code: <a id="a$id" onclick="check($id,1)" href="javascript:void(0)" class="black">Qualify</a> I want to remove the onclick="check($id,1) so the link cannot be clicked or "check($id,...

热门标签