2017-03-25 42 views
1

我调用函数document.write()的加入联内容(横幅广告):我页面上document.write()的()覆盖所有页面

<body> 
 
Page content start<br> 
 
    <script> 
 
    function postContent(){ 
 
     document.write('added content'); 
 
    } 
 
    postContent(); 
 
    </script> 
 
<br>Page content end 
 
</body>

Page content start 
added content 
Page content end 

我要添加这些内容有延迟,但里面的setTimeout()document.write()的覆盖所有的内容。

<body> 
 
Page content start<br> 
 
<script> 
 
    function postContent(){ 
 
     document.write('added content'); 
 
    } 
 
    setTimeout(function() { 
 
     postContent(); 
 
    }, 3000); 
 
</script> 
 
<br>Page content end 
 
</body>

页面上

3秒之内我:

added content 

我如何可以调用函数document.write()的有延迟和不覆盖所有的页面?

注意:我无权访问插入广告横幅的功能。

+0

你需要使用'document.write()的'本身? – DroidNoob

+0

insertAdjacentHtml呢? – funcoding

+0

完全加载HTML文档后使用document.write()将删除所有现有的HTML。 – CaptainHere

回答

0

如果您在加载文档后运行document.write - 它将覆盖整个文档anycase。所以,你应该选择一个特定的元素,并添加内HTML文本到它:

document.getElementById("container").innerHTML="added content"; 
0

你可以使用一个跨度和行写使用innerHTML来。

write()功能第一次工作,因为它加载与文档相同的时间,所以行之间添加行。

第二次重新加载文档删除所有的html。

<body> 
 
Page content start<br> 
 
<span id="pid"></span> 
 
<script> 
 
    function postContent(){ 
 
     document.getElementById("pid").innerHTML='added content'; 
 
    } 
 
    setTimeout(function() { 
 
     postContent(); 
 
    }, 3000); 
 
</script> 
 
<br>Page content end 
 
</body>