what is the meaning of the word “this” in Jquery script

前端 未结 3 539
余生分开走
余生分开走 2021-01-29 07:51

Hello I\'m a newcomer in JavaScript and JQuery language. I started to see some examples of JQuery script.

i have the following code segment:

 

        
3条回答
  •  礼貌的吻别
    2021-01-29 08:21

    • $('p') will add one or more paragraph elements (wrapped in jQuery magic) to an array
    • .click() is a jQuery function that will be called on each of the paragraph elements found (in the array)
    • function(){...} is the definition of that click event, where you can perform any javascript option when the paragraph is clicked
    • this is a global variable that refers to the calling DOM object, which I believe is window by default, but in this instance it would be each paragraph HTML element.

    Because you want to call a jQuery function (hide()) on the paragraph element, you have to wrap the base (HTML/DOM) object with all the jQuery functions, which is what $(this) does; it takes this and adds all the jQuery stuff to it, to turn it into a jQuery object, so that you can call all the jQuery functions. In other words:

    • this is the base object
    • $(this) is almost the same, but its a jQuery object, which inherits the object in scope, so that you can call all the jQuery sugar you want

提交回复
热议问题