如何使用data方法从元素中获取所有数据集? [英] How to get from an element all the data set using the data method?

查看:248
本文介绍了如何使用data方法从元素中获取所有数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

感谢您的帮助!

奥利维尔

推荐答案

之前已被问过.我的回答是这样的,因为这是一个好问题:

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

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

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.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'});

这篇关于如何使用data方法从元素中获取所有数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆