The distinction between the data () method, the ATT (), the prop () method in jquery

Source: Internet
Author: User

There are three ways in which jquery offers us

Data ()

Att ()

Prop ()

To set and get the properties of an object. But the design of these three methods does not repeat the useless design, then the three of their distinction is where it.

Demo

    

<div > I'm a div</div>.

  

  attr () Method: is the integration of the getattribute () and setattribute () methods in JS

Usage is the most routine.

$ (' div '). attr ({age:30,address: "Tianjin"});

var = $ (' div '). attr ("Age");

$ (' div '). Removeattr ("Age address"); If you want to delete multiple properties, the middle is separated by a space.

The property that is obtained by the attr () method can be seen in the HTML page check element.

  Prop () method: A more secure and covert approach

The use of setting properties and getting properties consistent with the attr () method.

    Different:

Part1: The property is obtained through the prop () method, which is not visible in the HTML page inspection element. This property exists in memory.

Part2:prop () The value is true/false when the Radio,select,checkbox is selected for the property operation

e.g.

$ (' Radio '). Prop (' checked ', true);

$ (' Radio '). attr (' checked ', "checked");

 Data () method: A more secure and covert approach

   In the HTML interface, check the elements, you can observe and modify the label's property values.

The properties that are set by the attr () method allow you to modify the Label property values of the HTML interface. But when it comes to financial interaction with the database, we can't allow the user to modify its value. The data () method is thus derived.

Way:

     

<div data-name = "Li" > I am a div</div>
By setting the data-(...) in the label The way to set the Data property.
Note: The contents behind the data- can only be in full uppercase or all lowercase. Recommended all lowercase.

$ (' Radio '). Data ("name"); To access the Data-name property of the label.

    

  

The distinction between the data () method, the ATT (), the prop () 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.