web123456

Use attributes starting with "data-" and data methods in JS to implement custom attributes

data-toggledata-The format at the beginning is placed on the element to represent the data carried by the element, such as:

1
< img  id = "img"  src = ""  data-bigimg = ""  />

JQuery’sdata() Methods can be operated directly, such as:

1
2
//Get:
var  bigImage = $( "#img" ).data( "bigimg" ); //Note that there is no need to add data here-
1
2
//set up:
$( "#img" ).data( "bigimg" , "" ); //Note that there is no need to add data here-

In this way, you can select all elements with the data-bigimg attribute in this way.


You can also pass directly$("#img").attr("data-bigimg"); To get and set



You will often see thedata-Attribute definition at the beginning,AlthoughW3CNot sure,But the latestHTML5Regulationdata-It's reasonable,existHTML5middle,Anydata-Custom properties are all started,It is usually used to implement someHTMLNo explicitly defined elements in,Apply user-defined properties to the code


Data ApiIt's a good one inDOMNew species that store data in objects, currently manyjsAll libraries use this feature. The specific implementation function depends on the call to this code.jsThe library is here.

Such as data-toggle,data-options