我正在编写一个脚本,当你刷新页面时会随机弹出一个任务。我有脚本工作,但是,当前的脚本将文本放在屏幕的左上角。
如何将我已有的内容转换为getElementById& innerHTML并在div中放置一个带有id =“quest_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>
答案 0 :(得分:2)
此处应该可行,第二个脚本代码将在<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>