V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
darasion
V2EX  ›  问与答

我想用 javascript 来顺次显示一条条的信息。

  •  
  •   darasion · 2010-10-31 22:40:59 +08:00 · 3952 次点击
    这是一个创建于 4950 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个页面有很多条目,保存在某个结构中。

    如果让这些东西载入后统一显示,给人的感觉很慢。
    所以我想让它们不是一次全部显示,而是随着html的载入,载入一条显示一条。

    例如:
    我做一个函数放在每个条目中,

    function showTopic(id){
    //....
    }

    然后一个 topic 列表,

    <ul>
    <li><script type="text/javascript">showTopic(1);</script></li>
    <li><script type="text/javascript">showTopic(2);</script></li>
    <li><script type="text/javascript">showTopic(3);</script></li>
    <li><script type="text/javascript">showTopic(4);</script></li>
    <li><script type="text/javascript">showTopic(5);</script></li>
    </ul>


    我试过用document.write(),静态载入的可以,直接就能显示。
    但是如果这条信息是通过ajax载入进来的,就会把整个页面都替换成它的内容,显然这个不符合要求。


    我该怎么做呢?
    1 条回复    1970-01-01 08:00:00 +08:00
    chone
        1
    chone  
       2010-10-31 23:18:43 +08:00
    <ul id="container">
    </ul>

    <script type="text/javascript">
    var containerEl = document.getElementById('container');

    var showTopic = function(id){
    var el = document.createElement('li');
    el.innerHTML = '<script type="text/javascript">showTopic(' + id + ');</script>';
    containerEl.appendChild(el);
    }

    </script>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1233 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:58 · PVG 01:58 · LAX 10:58 · JFK 13:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.