转换为getElementById

转换为getElementById

我写的脚本,将有一个随机弹出的任务时,你刷新页面。我让剧本开始工作了,但是现在的剧本把文本放在屏幕的左上角。
如何将已有的内容转换为getElementById&innerHTML之类的内容,并在要显示文本的div中放置一个I d=“quest\u id”的span标记?
我是一个javascript的初学者,我知道如何自己完成这些事情,但是将它们集成在一起会让我搞砸。通常我的问题是我还没有完全理解的简单语法。

<script language="JavaScript">



var str = "click to go to the quest";
var quest = str.link("index.html");


var r_text = new Array ();
r_text[0] = "You found a quest! - " + quest;
r_text[1] = " ";
r_text[2] = " ";
r_text[3] = " ";
r_text[4] = " ";
r_text[5] = " ";
r_text[6] = " ";
r_text[7] = " ";
r_text[8] = " ";
r_text[9] = " ";

var i = Math.floor(10*Math.random())

document.write(r_text[i]);

//-->
</script>

最佳答案

在这里,这应该可以工作,第二个脚本标记将在<body>之后加载,并且<div>将可用

var str = "click to go to the quest";
var quest = str.link("index.html");


var r_text = new Array ();
r_text[0] = "You found a quest! - " + quest;
r_text[1] = "1 ";
r_text[2] = " 2";
r_text[3] = " 3";
r_text[4] = " 4";
r_text[5] = " 5";
r_text[6] = " 6";
r_text[7] = " 7";
r_text[8] = " 8";
r_text[9] = " 9";

var i = Math.floor(10*Math.random())

document.write(r_text[i]);


//-->
</script>

<body>
    <div id="display">Here: </div>
</body>

<script language="JavaScript">

document.getElementById("display").innerHTML=r_text[i];
</script>

关于javascript - 将document.write转换为getElementById/innerHTML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29618640/

10-10 05:37