Difference between $(this) and this in jquery

前端 未结 7 2041
灰色年华
灰色年华 2020-11-28 07:40

What is the fundamental difference between using $(this) vs this

$(\'.viewComments\').click(function(ev){
    //returns the desired value
    alert(this.getA         


        
相关标签:
7条回答
  • 2020-11-28 07:42

    $(this) is a jQuery object and you can use the power and beauty of jQuery, but with 'this' keyword, one need to use native JavaScript.

    0 讨论(0)
  • 2020-11-28 07:46

    In jQuery, this refers to the DOM object, and $(this) refers to the same object but with jQuery methods added

    you can't call this.each() because each is not a DOM method, its a jquery method

    you can call $(this).each() because $(this) returns a jquery object

    0 讨论(0)
  • 2020-11-28 07:50

    this is the DOM object, whereas $(this) is the jQuery wrapper around same.

    When using this, you can call DOM methods on it, but not jQuery methods. When using $(this), you can call jQuery methods on it, but not DOM methods.

    0 讨论(0)
  • 2020-11-28 07:50

    in jQuery the $() notation is a shorthand for the jQuery selector, so if you say $(this) you are asking jQuery to re-select your object. Then you have the usual jQuery functions available.
    Then, this is the object selected by the outer jQuery call (JavaScript).

    0 讨论(0)
  • 2020-11-28 07:55

    $(this) - represent current DOM element on which event this function is called

    The this keyword - In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.

    0 讨论(0)
  • 2020-11-28 07:58

    $(this) is the current object that was selected using a jQuery selector or event attached to the object.

    so if you have $('#myelement').click(..... then $(this) referes to the element that was clicked on so that $(this).hide() hides that element.

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