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