jQuery 循环遍历 data() 对象

发布于 2024-07-17 06:17:10 字数 242 浏览 3 评论 0原文

是否可以循环遍历 data() 对象?

假设这是我的代码:

$('#mydiv').data('bar','lorem');  
$('#mydiv').data('foo','ipsum');  
$('#mydiv').data('cam','dolores');

我如何循环它? 可以使用 each() 来实现此目的吗?

Is it possible to loop through a data() object?

Suppose this is my code:

$('#mydiv').data('bar','lorem');  
$('#mydiv').data('foo','ipsum');  
$('#mydiv').data('cam','dolores');

How do I loop through this? Can each() be used for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

各空 2024-07-24 06:17:11

使用 jQuery 1.4 和 @user292614 的提示进行测试,效果如下:

$('#mydiv').data('bar','lorem');  
$('#mydiv').data('foo','ipsum');  
$('#mydiv').data('cam','dolores');

$.each( $('#mydiv').data(),function(i, e) {
   alert('name='+ i + ' value=' +e);
});

Tested with jQuery 1.4 and tips from @user292614 the following works:

$('#mydiv').data('bar','lorem');  
$('#mydiv').data('foo','ipsum');  
$('#mydiv').data('cam','dolores');

$.each( $('#mydiv').data(),function(i, e) {
   alert('name='+ i + ' value=' +e);
});
够运 2024-07-24 06:17:11

我不认为有任何函数可以为您提供使用 data() 函数添加的数据的所有“键”,但是为什么不将所有数据放入进入对象/映射下的函数?

像这样:

var container = new Object();
container.bar = "lorem";
container.foo = "ipsum";
container.cam = "dolores";
$("mydiv").data("container", container);

然后当你想读取数据/迭代它时:

var blah = $("mydiv").data("container");
for(key in blah) {
    var value = blah[key];
    //do whatever you want with the data, such as:
    console.log("The value of ", key, " is ", value);
}

I don't think that there is any function that gives you all of the "keys" of the data that has been added with the data() function, but instead, why not put all of your data into the function under an object / map?

something like this:

var container = new Object();
container.bar = "lorem";
container.foo = "ipsum";
container.cam = "dolores";
$("mydiv").data("container", container);

and then when you want to read the data / iterate through it:

var blah = $("mydiv").data("container");
for(key in blah) {
    var value = blah[key];
    //do whatever you want with the data, such as:
    console.log("The value of ", key, " is ", value);
}
黎夕旧梦 2024-07-24 06:17:11

我刚刚尝试过,但需要一些额外的数据值。 如果您也遇到这个“问题”,那么以下方法应该有效。

$('#mydiv').data('bar', {name:'lorem', id:'156', price:'199'}); 

那么你可以简单地使用值 id 进行扩展

$.each( $('#mydiv').data(),function(i, e) {
   alert('name='+ i + ' name=' +e.name + ' id='e.id + ' price=' + e.price );
});

I just tried this but needed some extra data values. If you also got this "problem" then the following should work.

$('#mydiv').data('bar', {name:'lorem', id:'156', price:'199'}); 

then you can simply extend with the value id

$.each( $('#mydiv').data(),function(i, e) {
   alert('name='+ i + ' name=' +e.name + ' id='e.id + ' price=' + e.price );
});
慕烟庭风 2024-07-24 06:17:11

如果我们使用 .data() 意味着它存储与匹配元素关联的任意数据,或者返回匹配元素集中第一个元素的指定数据存储中的值。

如果 .data() 在循环中,所以我们必须在循环中以相同的方式访问它,例如(如下)

<p class="weekday" data-today="monday">Monday</p>
<p class="weekday" data-today="tuesday">Tuesday</p>
<p class="weekday" data-today="wednesday">Wednesday</p>
<p class="weekday" data-today="thursday">Thursday</p>

我的循环中的 html 和 data-today 在所有标记中都是相同的,但它们的值不同所以基本上它是循环生成的 html 所以我们必须以相同的方式访问,即 js/jQuery 中的循环,例如 (在 jQuery 代码下面)

<代码>$('.weekday').each(function(){
$(this).data('今天');
});

OutPut :

Monday
Tuesday
Wednesday
Thursday

注意:在浏览器控制台中,它返回特定的

If we use .data() means it store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

and if .data() in loop so we have to access it same manner in loop so for e.g(below)

<p class="weekday" data-today="monday">Monday</p>
<p class="weekday" data-today="tuesday">Tuesday</p>
<p class="weekday" data-today="wednesday">Wednesday</p>
<p class="weekday" data-today="thursday">Thursday</p>

The html in my loop and data-today is same in all tags but their values are different so basically it is loop generated html so we have to access in same manner i.e loop in js/jQuery for e.g. (below jQuery code)

$('.weekday').each(function(){
$(this).data('today');
});

OutPut :

Monday
Tuesday
Wednesday
Thursday

NOTE : In browser console it return particular <DIV>.

夏日落 2024-07-24 06:17:10
$.each($.data(this), function(i, e) {
   alert('name='+ i + ' value=' +e);
});

这将迭代“this”元素的数据对象中的每个属性。

$.each($.data(this), function(i, e) {
   alert('name='+ i + ' value=' +e);
});

This will iterate through each property in 'this' element's data object.

回忆凄美了谁 2024-07-24 06:17:10

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

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

有了这个插件,您可以执行以下操作:

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

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

您可以仅使用 matt b 的建议,但这就是如何使用您现在拥有的东西来做到这一点。

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

You could just use matt b's suggestion but this is how to do it with what you have right now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文