Examples of attr () and prop () functions in jQuery (with usage differences) _ jquery

Source: Internet
Author: User
This article mainly introduces the usage of the attr () and prop () functions in jQuery, and analyzes the usage skills and precautions of the attr () and prop () functions in combination with the instance form, the difference between attr () and prop () functions is included. For more information about how to use attr () and prop () functions in jQuery, see the examples in this article. We will share this with you for your reference. The details are as follows:

I. jQuery's attr () method

In jquery, the attr () method is used to obtain and set element attributes. attr is the abbreviation of attribute. attr () and attr () are often used in jQuery DOM operations () there are four expressions.

1. attr (attribute name) // obtain the attribute value (obtain the attribute value of the First Matching Element. This method can be used to conveniently obtain the value of an attribute from the First Matching Element. Undefined is returned if the element does not have an attribute)

2. attr (attribute name, attribute value) // set the attribute value (set an attribute value for all matching elements .)

3. attr (attribute name, function value) // set the function value of the attribute (set a calculated attribute value for all matching elements. Instead of providing a value, a function is provided, and the value calculated by this function is used as the attribute value .)

4. attr (properties) // set multiple attribute values for the specified element, that is, {attribute name 1: "attribute value 1", attribute name 2: "attribute value 2 ",... ... }. (This is the best way to set multiple attributes in batches among all matching elements. Note: To set the class attribute of an object, you must use 'classname' as the attribute name. Or you can directly use 'class' or 'id '.)

Sample Code:

 Attr () method in jquery
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.