【发布时间】:2015-01-15 23:40:38
【问题描述】:
我正在使用这个 innerhtml 属性将我的 javascript 输出打印到 html 中的 div 中。我检查了其他有相同问题的主题,但似乎没有任何效果。
以下代码都在我的html中的'body'标签之间。
代码:
<script> function firstGame(); </script>
<div id="outputgame"> </div>
<button onclick="functionGame()"> Starten </button>
<script type="text/javascript">
function firstGame()
{
var output = document.getElementById('outputgame');
output.innerHTML('<p>First test</p>');
// Check if the user is ready to play!
confirm("Are you ready to play this epic game?")
var age = prompt("What is your age?");
if (age < 13)
{
document.getElementById('outputgame').innerHTML= "You are to young to play this game. You can proceed, however i do not take any responsibility";
}
else
{
document.getElementById('outputgame').innerHTML= "Sweet, Let's go!";
}
}
</script>
【问题讨论】:
-
你脑子里有没有提到jQuery?
-
您对
firstGame()的调用发生在与函数不同的脚本块中。所以当调用firstGame时会返回一个脚本错误,因为firstGame没有定义。将调用移至第二个脚本块。 -
你不需要 jQuery...
-
请删除确认,因为它显然没有必要而且非常烦人。
-
将提示包装在 parseInt(prompt("What is your age?"));
标签: javascript html output innerhtml