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

前端 未结 3 540
余生分开走
余生分开走 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:14

    It refers to each of p tags in your selector $("p") that gets clicked. For example, you can see html of each p tag you clicked on like this:

    $("p").click(function(){
       alert($(this).html());
    });
    

    Notice also that $(this) and this in above context mean different things. The latter this refers to DOM element itself which won't have jQuery methods/properties available to it, for example:

    $("p").click(function(){
       alert(this.html());
    });
    

    Won't work because html() won't be available because this refers to DOM element in there. So if you want to use jQuery methods, use $(this) instead.

提交回复
热议问题