how can i get all "rt_XX" with jquery each? I have no idea :/ .. Hope somebody can help me.
$("body").data('conf_pkc', 'normal');
$("body").data('conf_sst', '0');
$("body").data("rt_01", { tw:'k25', title:'Bernd', descr:'man' });
$("body").data("rt_12", { tw:'k115', title:'Hugo', descr:开发者_JS百科'man' });
$("body").data("rt_55", { tw:'k25', title:'Jan', descr:'man' });
/*
$.each( XXXXXXXXXXXX , function(k, v){
var rt_tw = $('body').data('rt_01').tw;
var rt_tw = $('body').data('rt_01').tw;
$('#d1').append('rt_tw -> '+rt_tw+'<br />');
$('#d1').append('rt_title -> '+title+'<br />');
$('#d1').append('rt_descr -> '+descr+'<br />');
});
*/
http://www.jsfiddle.net/V9Euk/543/
Thanks in advance. Peter
If you're using jQuery 1.4+, you should be able to do something like this:
var data = $("body").data();
for (p in data) {
if (p.substring(0,3) == "rt_") {
var x = data[p];
alert(x.tw);
alert(x.title);
alert(x.descr);
}
}
Just tested this at your link. Works as intended!
Good luck!
Since there's only one <body>
there's absolutely no need to be binding data to it (unless data is being utilised by some 3rd-party widget/plugin).
All data can be kept in some global configuration object or even global vars -- either will be better than the overhead of $('body').data('foo')
.
How about:
var rt = []; // An array
rt[12] = {...};
rt[55] = {...};
Note: you may want to use an object ({}
) instead of an array -- an object would be more friendly with a sparse array of values when looping.
$("body").data('conf_pkc', 'normal');
$("body").data('conf_sst', '0');
$("body").data("rt_01", { tw:'k25', title:'Bernd', descr:'man' });
$("body").data("rt_12", { tw:'k115', title:'Hugo', descr:'man' });
$("body").data("rt_55", { tw:'k25', title:'Jan', descr:'man' });
var b = $('body');
var data = b.data();
var arey = [];
for(var i in data) {
if(i.match(/rt_?\d{2}/) ) {
arey.push(i);
}
}
$.each(arey, function( index, val ) {
var item = $('body').data( val )
for(var i in item ) {
(function(t) {
console.log(val + ' => '+ t+ ' : ' +item[t]);
})(i);
}
});
Code tested on firebug, Works fine =)
Demo : http://jsbin.com/ufoza3
Try this:
value = jQuery.data($("body").find("xxx")[0], "rtxx)");
You'll need to loop through the elements if you don't use [0]. This is just to get you going.
精彩评论