用jquery的.each将数组循环出来放入html的div里改怎么写?

js: var arr = { one:1, two:2, three:3, four:4};
$.each(arr, function(key, val) {
//这里应该写什么代码???或者换一种方式写
});
html: <div id="thecity"></div>
最终想实现的html:<div id="thecity">1 2 3 4</div>应该怎么实现?

你定义是js方法
为啥用jquery each循环呢
你直接用for循环就是了 循环依据是arr的下标

然后吧值拼接一下用 以下都行
$("#thecity").text()
$("#thecity").html()
append(content | fn)
appendTo(content)
prepend(content | fn)
prependTo(content)
外部插入
after(content | fn)
before(content | fn)
insertAfter(content)
insertBefore(content)
包裹
wrap(html)
wrap(elem)
wrap(fn)
unwrap()
wrapAll(html)
wrapAll(elem)
wrapInner(html)
wrapInner(elem)
wrapInner(fn)
替换
replaceWith(content)
replaceAll(selector)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-05
$(function() {

var arr = { one:1, two:2, three:3, four:4};
var div_z = ""; //定义一个空变量
$.each(arr, function(key, val) {
//这里应该写什么代码???或者换一种方式写
div_z += val; //循环添加数组的value
});
$("#thecity").html(div_z); //通过div的id给div赋值!~

});

有问题可百度Hi我!good luck!
第2个回答  2011-12-06
$("#thecity").append(val);
如果要显示one:1, two:2, three:3, four:4
$("#thecity").append(i+":"+val);
第3个回答  推荐于2017-11-24
$('#thecity').append(val+' ');本回答被提问者采纳
第4个回答  2011-12-05
$("#thecity").append(val);

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网