两个for循环遍历多层嵌套的json数据时,只执行一层for循环,求解?

这是 json 数据

https://img.mukewang.com/5be141ba000105e902160540.jpg

ajax后 用for循环拼接

https://img4.mukewang.com/5be141cb0001ff8c14400286.jpg

大概的dom结构是:

https://img3.mukewang.com/5be141da0001963203440179.jpg

效果本来是第一个section里面的img里面放两张图片,第二发一张。
可是运行发现每个盒子里面都是三张。

小怪兽爱吃肉
浏览 1544回答 1
1回答

一只萌萌小番薯

不晓得是不是这样的效果。<body>&nbsp; <div class="main">&nbsp; &nbsp; &nbsp; <section class="image-text">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="say-lis-image-text"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="say-lis-new-title"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="img"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </section>&nbsp; </div></body><script>&nbsp; &nbsp; var html='';&nbsp; &nbsp; var $main = $(".main");&nbsp; &nbsp; for (var i in data) {&nbsp; &nbsp; &nbsp; &nbsp; html += '<section class="image-text">';&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; <div class="say-lis-image-text"><div class="title"><h3>'+data[i].content+'</h3></div></div>';&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; <div class="say-lis-new-title"><a href="#"><img src='+data[i].doctorAddress+'></a><span class="doc-name">'+data[i].doctorName+'</span></div>';&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; <div class="content">';&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>'+data[i].content+'</p>';&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="img">';&nbsp; &nbsp; &nbsp; &nbsp; //图片&nbsp; &nbsp; &nbsp; &nbsp; var imgs = data[i].img;&nbsp; &nbsp; &nbsp; &nbsp; for (var j in imgs){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html += '<div class="imgbox">'+ imgs[j].imgAddress+'</div>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>';&nbsp; &nbsp; &nbsp; &nbsp; html += '&nbsp; &nbsp; &nbsp; </div>';&nbsp; &nbsp; &nbsp; &nbsp; html += '</section>';&nbsp; &nbsp; }&nbsp; &nbsp; $main.html(html);</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript