About interviewing the difference between. attr () and. Prop () in jquery

Source: Internet
Author: User

Visual inspection Many people do not know this, I was also, know that these two things a little different, but it is not clear, then here, I would like to say in detail

As you all know, HTML tags can be written directly in the <> angle brackets, such as

$ (' img '). attr (' src ') or $ (' img '). Prop (' src ') Yes, it doesn't make any difference.

But one thing to note is that in HTML tags you can write some wonderful properties such as then how do you get this whatyourname attribute?

Use only $ (' img '). attr (' src '), if using. Prop () is not available.

That is, . attr () can get all the properties of a DOM element, including both the self-contained and the arbitrarily added properties of the background.

. Prop () can only get the properties of the DOM element's own.

So the question is, why?

We all know that jquery is based on the native JS, then the native JS how to get a picture of the src it?

There is a picture like this

JS gets can be in two ways document.getElementById (' pic '). src or document.getelmenetbyid (' pic '). getattribute (' src ')

Compared to the present should be very clear, directly through the. Src to get the image address is. Prop () in the encapsulated JS

The getattribute (' src ') to get the image address is the. attr () Encapsulated JS

So, in comparison, the efficiency of the. Prop () method should be higher than the efficiency of. attr (), well understood, but. attr () is more powerful and covers the. Prop ().

If you don't want to run efficiently, use it all. attr () All right.

Another problem is that the values obtained by. Prop () and. attr () are slightly different on the Disable property of the button, please see this article of the half old man for details http://www.cnblogs.com/Showshare/p/ Different-between-attr-and-prop.html

If you have any problems or mistakes, please contact me first.

About interviewing the difference between. attr () and. Prop () in jquery

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.