有人可以解释为什么document.write覆盖整个页面吗? [英] Could someone explain why this document.write overwrites the entire page?

查看:95
本文介绍了有人可以解释为什么document.write覆盖整个页面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码; javascript

Here is the code; javascript

function Roll()
     // Results: displays a randomly selected image of a 6-sided die
     {
       var roll;

       roll = RandomInt(1, 6);

       if (roll == 1) {
         document.getElementById("die").src = 
             "die1.gif";
         document.write("<center><p><a href='bad.html'> Enter </a></p></center>");
       }
       else if (roll == 2) {
         document.getElementById("die").src = 
             "die2.gif";
         document.write("<center><p><a href='good.html'> Enter </a></p></center>");
       }
       else if (roll == 3) {
         document.getElementById("die").src = 
             "die3.gif";
         document.write("<center><p><a href='bad.html'> Enter </a></p></center>");
       }
       else if (roll == 4) {
         document.getElementById("die").src = 
             "die4.gif";
         document.write("<center><p><a href='good.html'> Enter </a></p></center>");
       }
       else if (roll == 5) {
         document.getElementById("die").src = 
             "die5.gif";
         document.write("<center><p><a href='bad.html'> Enter </a></p></center>");
       }
       else {
         document.getElementById("die").src = 
             "die6.gif";
         document.write("<center><p><a href='good.html'> Enter </a></p></center>");
       }
     }

这是Jsfiddle http://jsfiddle.net/seymorevenue/TcNF7/(尽管document.write似乎在那里不起作用,并且骰子图像无法显示) 我认为我不太了解Document.write语句,还有更好的方法吗?我要做的就是滚动模具并根据结果显示链接.感谢您的帮助

And here is the Jsfiddle http://jsfiddle.net/seymorevenue/TcNF7/ (though document.write doesn't seem to work there, and the dice images arn't up) I don't think I really understand the Document.write statement, and is there a better method? All I want to do is roll a die and display a link based on the result. Thanks for any help

推荐答案

我认为document.write()对网站进行了重大更改,并且在单击按钮后很少使用,而是在网站加载时使用.

I think document.write() makes significant changes to a website and it is rarely used after clicking on a button, but rather when a site loads.

相反,请使用以下内容:

Instead, use something like this:

document.getElementByID('YourOutputDiv').innerHTML = 'Text to display on website';

这篇关于有人可以解释为什么document.write覆盖整个页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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