$this vs $(this) in jQuery

后端 未结 15 771
北恋
北恋 2020-11-29 18:07

I\'ve seen some discussions on SO regarding $(this) vs $this in jQuery, and they make sense to me. (See discussion here for an example.)

Bu

15条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 18:40

    Inside $.fn.lockDimensions, this is the jQuery object that had lockDimensions called on it.

    Inside the .each, this now references the DOMElement in the current iteration of the loop. $(this) wraps the DOMElement in a jQuery object, and var $this = $(this); is just saving $(this) in a variable called $this, so the jQuery constructor doesn't need to be called multiple times (if you were to use $(this) instead).

提交回复
热议问题