关于for循环的问题

来源:2-7 利用Ajax实现新闻列表

慕神9009311

2019-02-14 10:26:22

 var html = "";

           for(var i = 0; i < json.length; i++){

           var news = json[i];

           html = html + "<h1>"+news.title+"</h1>";

           html = html + "<h2>"+news.date+"&nbsp;"+news.source+"</h2>";

           html = html + "<hr/>"

          

           }

           document.getElementById("container").innerHTML = html;


这个document为什么不是写在循环体内呢?每循环一次 html之前接收到的值不是应该被覆盖掉了吗

写回答

1回答

好帮手慕阿莹

2019-02-14

并不是的呀,因为这里是要通过for循环展示这个列表,它相当于是拼在一起展示的。

然后再进行统一的写到id是contaner  的元素中去 document.getElementById("container").innerHTML = html;

比如:

http://img.mukewang.com/climg/5c64ffe90001e10609560536.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 10204 问题

查看课程