怎么用jquery 将json数组的第一个数据放到第一个div中,第二个数据放到第二个div中,以此类推
<html><head><scripttype="text/javascript"src="/jquery/jquery.js"></script><scripttype...
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var json = [{"name":"coffee","phone":"1234532"},
{"name":"Milk","phone":"7453252"},
{"name":"martin","phone":"2457788"},
{"name":"jerry","phone":"145564654"}];
$("button").click(function(){
$.each(json,function(index,content){
var name = content.name;
var phone= content.phone;
});
});
});
</script>
</head>
<body>
<button>输出</button>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
</body>
</html>
这是源码,第一条数据放到第一个ul里,第二条放到第二个ul里,要怎么做 展开
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var json = [{"name":"coffee","phone":"1234532"},
{"name":"Milk","phone":"7453252"},
{"name":"martin","phone":"2457788"},
{"name":"jerry","phone":"145564654"}];
$("button").click(function(){
$.each(json,function(index,content){
var name = content.name;
var phone= content.phone;
});
});
});
</script>
</head>
<body>
<button>输出</button>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
<ul>
<li id="li01">Coffee</li>
<li id="li02">Milk</li>
</ul>
</body>
</html>
这是源码,第一条数据放到第一个ul里,第二条放到第二个ul里,要怎么做 展开
1个回答
展开全部
$(document).ready(function(){
var json = [{"name":"coffee","phone":"1234532"},
{"name":"Milk","phone":"7453252"},
{"name":"martin","phone":"2457788"},
{"name":"jerry","phone":"145564654"}];
$("button").click(function(){
$.each(json,function(index,content){
//var name = content.name;
//var phone= content.phone;
$(body).append("<ul><li id="li01">"+content.name+"</li><li id="li02">"+content.phone+"</li></ul>");
});
});
});
var json = [{"name":"coffee","phone":"1234532"},
{"name":"Milk","phone":"7453252"},
{"name":"martin","phone":"2457788"},
{"name":"jerry","phone":"145564654"}];
$("button").click(function(){
$.each(json,function(index,content){
//var name = content.name;
//var phone= content.phone;
$(body).append("<ul><li id="li01">"+content.name+"</li><li id="li02">"+content.phone+"</li></ul>");
});
});
});
更多追问追答
追问
测试没反应,是不是只加一个$(body).append(""+content.name+""+content.phone+"");就可以?其他的地方要不要改?还有,能不能不写""这个,因为要是ul li有其他的样式,那不是很麻烦,能不能直接添加到里面去??
追答
$(body).append(""+content.name+""+content.phone+"");
这样应该就没问题了
可以不加
页面中就不需要写的了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询