The difference between JQuery this and $ (this) and how to get the $ (this) child element object

Source: Internet
Author: User

1.JQuery the difference between this and $ (this)

I believe many people who have just come into contact with jquery will have a vague distinction between $ (this) and this one, so what is the difference between the two?

First look at the $ () symbol in jquery, which in fact is the equivalent of jquery () in jquery, which is the $ (this) =jquery (), which means that you can return a jquery object. Well, when you are in the Web page alert ($ (' #id ')), a [object object] will pop up, which is the jquery object.

So, let's go back to the $ (this), what's this? Suppose we have the following code:

$ ("#desktop a img"). each (function (index) {

Alert ($ (this));

alert (this);

}

Well, this time you can see:

Alert ($ (this)); The result of the popup is [object Object]

alert (this); The bounce is [object Htmlimageelement]

That is, the latter returns an HTML object (in this case an IMG object that iterates through the HTML, so it is htmlimageelement).

Many people often this.attr (' src ') when using jquery. This will cause an error "object does not support this property or method", which is why? In fact, see the above example, we know where the wrong: very simple, this is the HTML object, then the HTML object can have the Val () method, so in use, we can not directly use this to directly invoke the method or property of jquery.

2. Get the method for the (this) child node object: Find (Element)

Understand the difference between $ (this) and this, and then take a look at this example: (Assuming that my page contains an img in a tag and contains the SRC attribute), when I traverse, I want to fetch the address of the SRC in the IMG under the $ (this)

$ ("#desktop a"). each (function (index) {

var imgurl=$ (this). FIND (' img '). attr (' src ');

alert (Imgurl);

}

where. Find (Element) is a DOM element that returns a matching element so that it can fetch the desired SRC address.

Source: http://www.cnblogs.com/hannover/p/4109779.html

The difference between JQuery this and $ (this) and how to get the $ (this) child element object

Related Article

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.