javascript更改innerhtml

问题描述:

好吧我是javascript的新手,但我试图更改div标签的innerhtml,继承我的脚本并且它不起作用:

ok im new at javascript, but im trying to change the innerhtml of a div tag, heres my script and its not working:

<head>
<script type="text/javascript">
function var1() {
document.getElementById('test').innerHTML = 'hi';
}
window.onLoad = var1();
</script>
</head>
<body>
<div id="test">change</div>
</body>

它应该有效,但由于某些原因它没有,有什么帮助吗?

it should work but for some reason its not, any help?

而不是将 var1 分配给 window.onload ,您当前正在调用该函数并存储其结果。此外,这可能是显而易见的,但 var1 似乎是函数的奇怪名称。试试这个:

Rather than assigning var1 to window.onload, you're currently calling the function and storing its result. Also, this might be obvious, but var1 seems like an odd name for a function. Try this:

function var1() {
  document.getElementById('text').innerHTML = 'hi';
}

window.onload = var1;

注意 onload 的大小写,以及作为 var1 之后缺少的括号。

Note the casing of onload, as well as the missing parentheses after var1.