Use instructions for this in jquery _jquery

Source: Internet
Author: User

Original JS in this so easy to use

Copy Code code as follows:

$ (document). Ready (function () {
var dragelement=null;
$ ("#zz"). MouseDown (function () {
dragelement=this;//at this point this is the $ ("#zz") object
$ (document). MouseMove (function () {
If the direct $ (this). CSS ("left")//is within the MouseMove, so that this object is not $ ("#zz"),
You can use $ (dragelement). CSS ("left")//So take is $ ("#zz")
})
})
})


$ (this) what is generated

What is generated by $ ()? In fact $ () =jquery (), then that means returning a jquery object.
Digression: Usually we use $ () directly for simplicity, in effect, the function omits a parameter context, that is $ (selector) =$ (selector,document). If the context is specified, You can specify that the context is a DOM element set or a jquery object.

So according to the conclusion that $ () returns a JQuery object, we can conclude that $ (this) gets a jquery object. We can print an object using the Universal Alert () method:
Alert ($ (' #btn '));
The results displayed:

The red box of the figure is an object, which is naturally the object of jquery. It means that we use $ (' #btn ') to invoke jquery's methods and properties.

What does this represent?

This, the programmer knows that this represents the object that the context is in, which is good, but what is this object? Add JS inside also have GetType words return value will be what? In fact, JS inside do not need to use GetType, because we have omnipotent alert. Take a look at the following code:
Copy Code code as follows:

$ (' #btn '). Bind ("click", Function () {

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

According to our experience (since $ () is the object of jquery), this is naturally a jquery object. But let's look at the results returned:


What's the return? "Object Htmlinputelement"--Great HTML object, hehe. So we usually report an error when we use This.val () directly or through this to invoke the method or property peculiar to jquery: Why? Rhetorical The HTML object, of course, "does not contain attributes 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 jquery's API documentation, it looks like jquery does not have a special "deal" for this keyword, meaning that this is in JS, not jquery redefined. So ... Of course this is only my own idea, if there is a better understanding of friends can leave a message correction. And let's take a look at alert ($ (this)) in the above code, which is naturally a jquery object, calling jquery's unique methods and properties.

Conclusion:

This indicates that the current context object is an HTML object that can invoke the properties owned by the HTML object, method
$ (this), the context object represented is a jquery context object that can invoke jquery's method and property values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.