Converting JavaScript 'this' to jQuery '$(this)'

后端 未结 3 1304
长发绾君心
长发绾君心 2021-01-04 12:29

Please have a look at the following code:


    
        

        
3条回答
  •  梦毁少年i
    2021-01-04 13:07

    Try this:

    $(document).ready(function(){
       $('ul li').click(function(){
           alert($(this).index());
       });
    });
    

    If you want to read over all the elements in the list you can use:

    $(document).ready(function(){
        $('ul li').each(function(){
            alert($(this).index());
        });
    )};
    

    Hope this helps.

提交回复
热议问题