Home > Web Front-end > JS Tutorial > Discussion on jQuery's data api issue when the custom data attribute is a json format string_javascript skills

Discussion on jQuery's data api issue when the custom data attribute is a json format string_javascript skills

WBOY
Release: 2016-05-16 17:41:53
Original
1140 people have browsed it

jQuery's data API implementation has the effect of caching data
Using IE 7 (IE8 switches to IE7 mode in the console), when the DOM node has custom data attributes, you can check the DOM node to see attributes in the shape of jQuery18305664906559272507 , which is the construction used to get custom data from the data storage object.

When the custom data attribute is a json format string, if the cached data is modified, the modified data will continue to exist in the cache system. If you are not careful, this may cause some BUG

Copy code The code is as follows:








< ;script>
var node = $("#tst");
console.log(node.attr("data-json"));
var data = node.data("json") ;
console.log(data);
data.b = 'hello';
var data1 = JSON.parse(node.attr("data-json"));
console.log (data1);
console.log('data===data1',data===data1);
var data2 = node.data("json");
console.log(data2) ;
console.log('data===data2',data===data2);
var data3 = JSON.parse(node.attr("data-json"));//Get the value like this Not affected by caching
console.log(data3);




Running results
Copy code The code is as follows:

{"a":123,"b":456}
Object {a: 123, b: 456}
Object {a: 123, b: 456}
data===data1 false
Object {a: 123, b: "hello"}
data===data2 true
Object {a: 123, b: 456}
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