javascript document.write()从页面中删除html并在空白页面中显示结果 [英] javascript document.write() removes the html from page and display result in a blank page

查看:155
本文介绍了javascript document.write()从页面中删除html并在空白页面中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript - document.write的替代品是什么?

我正在创建一个javascript函数,我希望在几秒钟后执行它但是当它执行时它会删除所有页面内容并仅显示我使用document.write()显示的结果这里是我的javascript代码。

i am creating a javascript function which i want to execute after few seconds but when it executes it removes all the page content and display only that result which i am displaying using document.write() here is my javascript code.

<script language="javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

setTimeout(function(){
xmlhttp.open("GET","some.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("offer");
var page = parseInt(x.length) / 10;
document.write("<div class='pagination_btn_cont'>");
for (i=1;i<=page;i++)
{ 
 document.write("<div class='pagination_btn'>"+i+"</div>");
}
document.write("</div>");
},10000);

</script>

当我打开网页时,它显示页面的所有内容,但10秒后页面将变为空白并仅显示我从循环中获得的数字。

when i open the webpage its display all the content of the page but after 10 seconds the page will become blank and display only the numbers which i am getting from the loop.

任何建议如何完成此任务。

any suggestion how can do this task.

推荐答案

你可以在javascript中使用innerHTML。
用于在不影响页面内容的情况下将数据插入特定div。

You can use innerHTML in javascript. It use to insert data to particular div without affecting page contents.

示例:

var results = "";
for(var i=1;i<=10;i++)
{
     results += "<div class='pagination_btn'>"+i+"</div>";
}
document.getElementById("your result show div id").innerHTML = results;

您可以指定 $('。pagination_btn')。bind (点击)... 在您的文档中。已经

you can specify $('.pagination_btn').bind("click")... inside your document.ready

这篇关于javascript document.write()从页面中删除html并在空白页面中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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