JavaScript innerHTML不起作用 [英] JavaScript innerHTML not working
本文介绍了JavaScript innerHTML不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个非常简单的程序,输出应该是 s
和JavaScript,但我只得到 a
。
Here is a very simple program and the output should be s
and JavaScript but I am only getting a
.
<html>
<head>
<title></title>
<script type="text/javascript">
document.getElementById("ma").innerHTML="JavaScript";
</script>
</head>
<body>
<h1 id="ma">s</h1>
</body>
</html>
推荐答案
当你没有这个元素时重新尝试设置一个值。在< h1>
添加到DOM后,您需要调用此方法。
The element doesn't exist at the time you're attempting to set a value. You need to call this after the <h1>
has been added to the DOM.
您可以移动这个< script>
进一步标记,或者将您的逻辑添加到文档加载时应该调用的函数中:
You can either move this <script>
tag down further, or add your logic to a function which ought to be called when the document has been loaded:
window.onload = function() {
/* Add your logic here */
}
演示: http://jsfiddle.net / Lr2Hm /
这篇关于JavaScript innerHTML不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文