Jquery getting custom attributes (attr and prop) instance introduction _ jquery

Source: Internet
Author: User
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 jQueryDOM operations () there are 4 expressions $ ("form "). attr ("check"); $ ("form "). prop ("check"); both of them are acceptable, but the new version of jquery recommends the second one. Both of them are similar in other aspects. The only difference I found is that in checkbox, you need to use prop, otherwise IE browser will be incompatible

The Code is as follows:





Script








Script
// Var J = $ ("p [lang]"). get ();
// Alert ($ ("[data-url]: eq (2)"). attr ("data-url "));
$ ("[Data-url]"). each (function (){
Alert ($ (this). attr ("data-url "));});
// $ ("[Data-url]"). each (function (){
// Alert ($ (this). prop ("data-url "));//});
Script


Appendix: jquery 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:

The Code is as follows:






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