Home  >  Article  >  Web Front-end  >  Instructions for using this in jquery_jquery

Instructions for using this in jquery_jquery

WBOY
WBOYOriginal
2016-05-16 18:20:121091browse

It turns out that this in js is so easy to use

Copy code The code is as follows:

$ (document).ready(function(){
var DragElement=null;
$("#zz").mousedown(function(){
DragElement=this;//This refers to $("#zz") object
$(document).mousemove(function(){
If directly $(this).css("left")//Since it is inside mousemove, this object will Not $("#zz"),
In this case, you can use $(DragElement).css("left")//What you get is $("#zz")
})
})
})


What does $(this) generate?

What does $() generate? In fact, $()=jquery() means that a jquery object is returned.
Digression: Usually we use $() directly for simplicity. In fact, this function omits a parameter context, that is, $(selector)=$(selector,document). If you specify context, you can specify context as a dom Set of elements or jquery object.

According to the conclusion that $() returns a jquery object, we can conclude that $(this) returns a jquery object. We can use the universal alert() method to print out an object:
alert($('#btn'));
Results displayed:
Instructions for using this in jquery_jquery
The red box in the picture is an object. Don’t think about it. The object is naturally jquery object. That is to say, we use $('#btn') to call jquery's methods and properties.

What does this stand for?

this, programmers know that this represents the object where the context is located. This is naturally good, but what exactly is this object? If there is also getType in js, what will be the returned value? In fact, there is no need to use getType in js, because we have a universal alert. Please look at the following code:
Copy code The code is as follows :

$('#btn').bind("click",function(){

alert(this);
alert($(this));
});

According to our experience (because $() generates a jquery object), this is naturally a jquery object. But let’s take a look at the returned results:
Instructions for using this in jquery_jquery

What is returned? [object HTMLInputElement]——Great html object, hehe. So we usually get an error when we use this.val() directly or call jquery-specific methods or properties directly through this: Why? Asking questions knowingly! Of course, the html object "does not contain properties or methods". So why does calling this in the context of a jquery object return an html object instead of a jquery object? Looking through the API documentation of jquery, it seems that the keyword this has not been specially "processed" in jquery, which means that this is in js, not redefined by jquery. So... Of course, this is just my own idea. If anyone knows more about this, you can leave a message and correct it. Let's take a look at the return of alert($(this)); in the above code. It is naturally an object of jquery. There is no problem at all in calling jquery's unique methods and properties.

Conclusion:

this, indicating that the current context object is an html object, and you can call the attributes owned by the html object, the method
$(this), The represented context object is a jquery context object, which can call jquery's methods and attribute values.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn