document.getElementById(“xxxxx”).innerHTML isn't working at all?

你说的曾经没有我的故事 提交于 2019-12-02 10:14:13

At the time you execute the script and you're looking for the element, it does not exist yet because it comes later on.

To be precise, it has not been parsed and added to the DOM yet.

So move your scripts to the bottom of the HTML file (at least below the element).

If you are using some kind of library, it can be solved easily too: for jQuery for example, place everything inside a $(function() { ... } block. That way, it isn't until the DOM has fully loaded that it gets executed (at which time you can safely search for the element).

On the moment when you are trying to access the #game element it is not yet created. You should wait for the onload event and bind the function to this event:

<script type="text/javascript">
function init() {
   /**your code here**/
}
</script>

<body onload="init()">
<div id="game"></div>
</body></html>

The Init() function will not work, because the DOM is not entirely loaded. Either you get yourself jquery or dojo and use the document ready functions, or place the javascript block at the end of your html document, aka before the closing body tag.

Get yourself Firebug, and rewrite the code like this for the beginning:

<html>
<body>
<div id="myid"></div>
</body>
<script language="Javascript">
document.getElementByID('myid').innerHTML = ....
</html>

Tamer

or you can use jqueries

$(document).ready(function(){});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!