如何使用数据方法从元素中获取所有数据集?

时间:2022-09-25 23:20:51

all is said in the title, I want to get from an element all the data set using the data method.

所有都在标题中说,我想从一个元素中获取使用数据方法的所有数据集。

(ultimately I want to copy that data over to a newly created element)

(最终我想将数据复制到新创建的元素)

thanks for any help !

谢谢你的帮助 !

Olivier

1 个解决方案

#1


This has been asked before. My answer from there, since it is a good question:

以前曾经问过这个问题。我的回答是,因为这是一个很好的问题:

jQuery stores all the data information in the jQuery.cache internal variable. It is possible to get all the data associated with a particular object with this simple but helpful plugin:

jQuery将所有数据信息存储在jQuery.cache内部变量中。使用这个简单但有用的插件可以获取与特定对象关联的所有数据:

jQuery.fn.allData = function() {
    var intID = jQuery.data(this.get(0));
    return(jQuery.cache[intID]);
};

With this in place, you can do this:

有了这个,你可以这样做:

$('#myelement').data('test1','yay1')
               .data('test2','yay2')
               .data('test3','yay3');

$.each($('#myelement').allData(), function(key, value) {
    alert(key + "=" + value);
});

Alternatively, you can simply store an object:

或者,您可以只存储一个对象:

$('#myelement').data('data', {test1:'yay1',test2:'yay2',test3:'yay3'});

#1


This has been asked before. My answer from there, since it is a good question:

以前曾经问过这个问题。我的回答是,因为这是一个很好的问题:

jQuery stores all the data information in the jQuery.cache internal variable. It is possible to get all the data associated with a particular object with this simple but helpful plugin:

jQuery将所有数据信息存储在jQuery.cache内部变量中。使用这个简单但有用的插件可以获取与特定对象关联的所有数据:

jQuery.fn.allData = function() {
    var intID = jQuery.data(this.get(0));
    return(jQuery.cache[intID]);
};

With this in place, you can do this:

有了这个,你可以这样做:

$('#myelement').data('test1','yay1')
               .data('test2','yay2')
               .data('test3','yay3');

$.each($('#myelement').allData(), function(key, value) {
    alert(key + "=" + value);
});

Alternatively, you can simply store an object:

或者,您可以只存储一个对象:

$('#myelement').data('data', {test1:'yay1',test2:'yay2',test3:'yay3'});