jQuery Passing $(this) to a Function

后端 未结 4 1653
轻奢々
轻奢々 2021-01-30 20:11

I have lines of code like this:

$(this).parent().parent().children().each(function(){
    // do something
});

It works well. But I need to run

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 20:42

    jQuery will automatically invoke your function with the proper context set.

    $('#button').on('click', myFunction);
    
    function myFunction() {
        var that = $(this);
        console.log(that);
    }
    

提交回复
热议问题