Use of 'this' in closure

前端 未结 2 1158
暗喜
暗喜 2021-01-23 06:42

I\'m just curious... how am I supposed to use \'this\' within a jQuery function?

For example, if I have some code like this...

    headEl.find(\"form.blo         


        
相关标签:
2条回答
  • 2021-01-23 07:02

    I would say ignore jslint personally. If you really wanted to get rid of the warning you could probably do this:

    headEl.find("form.blog-search input").focus(function(e) {
        $(e.currentTarget).next("span").animate({opacity:1}, 200);
    })
    

    The function passed to focus gets passed the event object.

    http://api.jquery.com/event.currentTarget/

    0 讨论(0)
  • 2021-01-23 07:02

    There is no issue which your use of this in this particular closure.

    The warning is probably because sometimes people expect the value of this to be the same as it was outside the closure and that is often not the case.

    In your particular case you are using it correctly as the event system sets this to be the object that triggered the event.

    For example, people often do an ajax call and expect the value of this to be the same inside the success handler as it was when the ajax call was made, but that is usually not the case.

    0 讨论(0)
提交回复
热议问题