innerHTML导致ul自关闭 [英] innerHTML causing ul to self-close

查看:67
本文介绍了innerHTML导致ul自关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从缓存的文本文件中提取并循环浏览这些推文来显示Twitter提要.正常工作,直到我尝试将这些推文转换为列表. li元素正确地附加到innerHTML上,但是开头ul自动关闭,结尾ul消失.

I'm trying to display a Twitter feed by pulling from a cached text file and looping through the tweets. Works fine until I try to turn these tweets into a list. The li elements append to the innerHTML properly, but the opening ul self-closes, and the ending ul disappears.

function twitterStatusCallback(obj) {
   if(obj && obj.length){
       document.getElementById('twitter_status').innerHTML += "<ul>";
       var len = obj.length;

      for(var i = 0; i < len; i++){
    //Only get tweets less than 7 days old.
    if ((relativeTimeNumeric(obj[i].created_at)) < (8*24*60*60)) { 

    var tweet = obj[i];

        document.getElementById('twitter_status').innerHTML += "<li>" + linkify(tweet.text);
        document.getElementById('twitter_status').innerHTML += relativeTime(tweet.created_at) +"</li>";

   }
   }
  document.getElementById('twitter_status').innerHTML += "</ul>"; 
}
 }

推荐答案

如何构建一个字符串,然后在完全构建字符串之后将其分配给innerHTML(这样就永远不会注入损坏的html).一点一点地向浏览器中发射损坏的html势必会引起问题,因为浏览器无法存储损坏的dom树.

How about building a string and then assigning it to innerHTML when it's completely built (so you never inject broken html). Firing broken html into the browser bit by bit is bound to cause problems, because the browser cannot store a broken dom tree.

这篇关于innerHTML导致ul自关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆