news.js

来自「源码JAVASCRIPT精粹」· JavaScript 代码 · 共 45 行

JS
45
字号
var items = [    'First news item',    'Second news item',    'Third news item',    'Fourth news item',    'Fifth news item',    'Sixth news item',    'Seventh news item',    'Eighth news item',    'Ninth news item',    'Tenth news item'];var count = 0;function updateNews(){  var list = document.getElementById('news');  while (list.childNodes.length > 0)  {    list.removeChild(list.firstChild);  }  for (var i = count; i < count + 5; i++)  {    var n = i >= items.length ? i - items.length : i;    var li = document.createElement('li');    var a = li.appendChild(document.createElement('a'));    a.href = '#';    a.appendChild(document.createTextNode(items[n]));    list.appendChild(li);  }}window.onload = function(){  updateNews();  window.setInterval(function()  {    count++;    if (count == items.length) { count = 0; }    updateNews();  }, 5000);};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?