Home > Web Front-end > JS Tutorial > Detailed explanation of jQuery.removeData() function usage

Detailed explanation of jQuery.removeData() function usage

巴扎黑
Release: 2017-06-25 09:51:03
Original
2315 people have browsed it

The

removeData() function is used to remove the data item with the specified key stored on each element matched by the current jQueryobject.

The removeData() function is mainly used to remove data stored through the data() function.

This function belongs to the jQuery object (instance).

Syntax

The syntax of the removeData() function is as follows:

jQueryObject.removeData( keys )

Note: removeData( ) will remove the data with the specified key on each element matched by the current jQuery object.

Parameters

Parameter Description

keys Key name specified by String/Array type String or Array.

If you want to remove data items with multiple key names at the same time, please pass in the parameter in the form of an array. Each element of the array is the key name string that needs to be removed. You can also pass in a space-separated string. Each substring separated by spaces is the key name string that needs to be removed.

If the parameter keys is a string containing spaces, such as "a b c", then removeData() will first determine whether there is a data item with the key name "a b c" (the string itself), and if it exists, remove it This data will no longer perform subsequent segmentation and other operations. If it does not exist, it will be split based on spaces, and the data items whose key names are "a", "b", and "c" (split substrings) will be removed.

If the key name where you store data contains spaces (such as "a b"), use removeData() to remove data items with multiple key names at the same time (such as "a b c d", where "a b" is a key name), key names containing spaces (such as "a b") will not be successfully removed. You can use an array instead, or individually remove key names that contain spaces.

Return value

removeData()The return value of the function is of jQuery type and returns the current jQuery object itself.

Example & Description

Take the following HTML code as an example:

;li id="n5">item2

;li id="n6">item3

##

We write the following jQuery code:

var $li = $("li");

// Store data to all li elements at the same time

$li.data("name", "CodePlayer");

$li.data("desc", "Focus Sharing on programming development technology");

$li.data("url", "http://www.365mini.com/");

// Remove all li at the same time The data with the key name name on the element

$li.removeData("name");

var $n5 = $("#n5"); // Through n4, n5, n6 can read data

// Return the data corresponding to the key value name

document

.writeln( $n5.data("name") ); / / undefined

var $n4 = $("#n4");

// The data item with the key name "desc url" cannot be found, and then split based on spaces // Remove data items with key names "desc" and "url"

$n4.removeData("desc url");

document.writeln( $n4. data("desc") ); // undefined

document.writeln( $n4.data("url") ); // undefined

$li.data("a", "Test a");

$li.data("b", "Test b");

$li.data("a b", "Test a b");

//Only the data item with the key name "a b" will be removed

$li.removeData("a b");

document.writeln( $li.data( "a") ); // Test a

document.writeln( $li.data("b") ); // Test b

The above is the detailed content of Detailed explanation of jQuery.removeData() function usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template