Home >Web Front-end >JS Tutorial >Example of usage of removeData() method in jQuery

Example of usage of removeData() method in jQuery

PHPz
PHPzOriginal
2016-05-16 16:24:131732browse

This article mainly introduces the usage of the removeData() method in jQuery. It analyzes the technique of removeData() method to remove the specified data of matching elements in the form of examples. It has certain reference value. Friends who need it can refer to it

The example in this article describes the usage of the removeData() method in jQuery. Share it with everyone for your reference. The specific implementation method is as follows:

This method can remove the specified data on the matching element.
The removeData() method has the opposite effect to the data() method.

Grammar structure one:

$(selector).removeData(name)

Parameter list:

Parameters
参数 描述
name 可选。定义要删除的数据的名称。
     如果没有定义名称,该方法将从被选元素中删除所有已存储的数据。
Description

name Optional. Defines the name of the data to be deleted. If no name is defined, this method will delete all stored data from the selected elements.
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="//www.jb51.net/" />
<title>脚本之家</title> 
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("#add").click(function(){ 
    $("p").data("mydata","脚本之家欢迎您"); 
  }); 
  $("#show").click(function(){ 
    $("p").text($("p").data("mydata")); 
  }); 
  $("#delete").click(function(){ 
    $("p").removeData("mydata"); 
    alert($("p").data("mydata")); 
  }) 
}); 
</script> 
</head> 
<body> 
  <p></p> 
  <button id="add">向元素添加数据</button> 
  <button id="show">显示添加的数据</button> 
  <button id="delete">删除元素中的数据</button> 
</body> 
</html>

Example code:

The above code can be specified through the data() method The element appends data, then uses the removeData() method to delete the data, and finally detects whether the data has been deleted.

The above is the entire content of this chapter. I hope it will be helpful to everyone’s jQuery programming. For more related tutorials, please visit jQuery Video Tutorial!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn