jquery data() method


  Translation results:

data

English [ˈdeɪtə] US [ˈdetə, ˈdætə, ˈdɑtə]

n. Information, material; plural of datum; [computer] data, information ;Value extracted from scientific experiments

jquery data() methodsyntax

Function: data() method appends data to the selected element, or obtains data from the selected element.

Return data from the element: Return additional data from the selected element.

Syntax: $(selector).data(name)

Parameters: name Optional. Specifies the name of the data to be retrieved. If no name is specified, this method returns all stored data from the element as an object. Append data from element: Append data to the selected element.

Syntax: $(selector).data(name,value)

##Parameters: name Required. Specifies the name of the data to be set. value Required. Specifies the value of the data to be set.

Use objects to append data to elements: Use objects with name/value pairs to add data to selected elements.

Syntax: $(selector).data(object)

Parameters: object required. Specifies an object containing name/value pairs.

jquery data() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn1").click(function(){
    $("div").data("greeting", "Hello World");
  });
  $("#btn2").click(function(){
    alert($("div").data("greeting"));
  });
});
</script>
</head>
<body>
<button id="btn1">把数据添加到 div 元素</button><br />
<button id="btn2">获取已添加到 div 元素的数据</button>
<div></div>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance

Popular Recommendations

Home

Videos

Q&A