简体   繁体   English

动态创建更新的html列表

[英]Dynamically create a html list that update

I'm trying to dynamically create a html list that update when new information arrives. 我正在尝试动态创建一个HTML列表,该列表会在收到新信息时更新。 Here is my code : 这是我的代码:

 setlast(); function setlast() { last = [ "xyyxx", "yxyxx", "xxxxy", "yyxxy", "yxyxy"] }; setInterval(function() { last[0] = Math.random(); last[1] = Math.random(); last[2] = Math.random(); last[3] = Math.random(); last[4] = Math.random(); }, 2000); createHtml(); function createHtml() { setTimeout(function(){ ul = document.createElement("ul"); document.body.appendChild(ul); ul.setAttribute("id", "lyl"); for(w=0; w<5; w++) { li = document.createElement("li"); ul.appendChild(li); } updateT(); },3500); } function updateT() { setInterval(function() { li.innerHTML = last[w]; },2000); } 
 #listwrapper { margin-top: 2%; width : 100%; } li { display : inline; float : left; padding : 10px; } 

Doing like above, I get undefined. 像上面一样,我不确定。

If I do it like below, the code works but each li do not update every time Math.random generates a new number.: 如果我像下面那样做,代码可以工作,但是每次Math.random生成新数字时,每个li都不会更新。

 setlast(); function setlast() { last = [ "xyyxx", "yxyxx", "xxxxy", "yyxxy", "yxyxy"] }; setInterval(function() { last[0] = Math.random(); last[1] = Math.random(); last[2] = Math.random(); last[3] = Math.random(); last[4] = Math.random(); }, 2000) createHtml(); function createHtml() { setTimeout(function(){ ul = document.createElement("ul"); document.body.appendChild(ul); ul.setAttribute("id", "lyl"); for(w=0; w<5; w++) { li = document.createElement("li"); ul.appendChild(li); li.innerHTML = last[w]; } },3500) } 
 #listwrapper { margin-top: 2%; width : 100%; } li { display : inline; float : left; padding : 10px; } 
 

So my question is : how can I get the list to display and update every time a new number is generated ? 所以我的问题是 :每次生成新号码时,如何显示和更新列表?

Thanks a lot. 非常感谢。

Here is a working example using only pur javascript : 这是仅使用pur javascript的工作示例:

 /** * Update The HTML * * @param {Array} last - ensure the array exist by passing it as an argument */ function updateHtml(last) { const ul = document.querySelector('#toUpdate'); // Empty existing li node ul.innerHTML = '' for (let i = 0; i < last.length; i++) { // create and append li elements const li = document.createElement('li') li.innerHTML = last[i] ul.appendChild(li); } } setInterval(function() { // call with random stuff updateHtml([ Math.random(), Math.random(), Math.random(), Math.random(), Math.random() ]) }, 2000) // first call with strings updateHtml([ "xyyxx", "yxyxx", "xxxxy", "yyxxy", "yxyxy" ]); 
 #listwrapper { margin-top: 2%; width: 100%; } li { display: inline; float: left; padding: 10px; } 
 <div id="main"> <ul id="toUpdate"> </ul> </div> 

There is many things to say about DOM Update but most of all what I've done is to exctract the update function to specialize it so updateHtml manipulate the DOM according to the 'last' argument. 关于DOM更新,有很多事情要说,但是我所做的大部分工作都是吸引更新功能来专门化它,因此updateHtml根据'last'参数来操纵DOM。

Please note that this is a one way data binding. 请注意,这是数据绑定的一种方式。 You can do more by using more complex structures and patterns like Observable. 您可以通过使用更复杂的结构和模式(例如Observable)来做更多的事情。

Hope that help 希望对您有所帮助

 setlast(); function setlast() { last = [ "xyyxx", "yxyxx", "xxxxy", "yyxxy", "yxyxy"] }; setInterval(function() { last[0] = Math.random(); last[1] = Math.random(); last[2] = Math.random(); last[3] = Math.random(); last[4] = Math.random(); }, 2000); createHtml(); function createHtml() { setTimeout(function(){ ul = document.createElement("ul"); document.body.appendChild(ul); ul.setAttribute("id", "lyl"); for(w=0; w<5; w++) { li = document.createElement("li"); ul.appendChild(li); } updateT(); },3500); } function updateT() { setInterval(function() { list = ul.childNodes; for (var i = 0; i < list.length; i++) { list[i].innerHTML = last[i]; } },2000); } 
 #listwrapper { margin-top: 2%; width : 100%; } li { display : inline; float : left; padding : 10px; } 

This works. 这可行。 Notes: 笔记:

  1. Create variables without keywords let or var makes them global which is a bad thing. 创建没有关键字letvar变量会使它们成为全局变量,这是一件坏事。 Variables should be accessible only where its supposed to. 变量应仅在其应有的位置可访问。 See this post. 看到这篇文章。
  2. The above code will fail if the number of elements in the last list is less then the number of li elements appended to ul . 如果last列表中的元素数少于附加到ulli元素数,则上述代码将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM