开发者

why the javascript code doesn't work? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.
<!DO开发者_StackOverflow社区CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
    <script type="text/javascript">
    window.onload = function(){
        document.getElementById("myMessage").innerHTML = "hello,world!";
    }
</script>
</head>

<body>
    <h1 id=="myMessage"></h1>

</body>
</html>

when i run the file, there is no hello,world! output. why?


Your script block shoul be in the <head> or <body>, not outside either.

It's hard to keep track of your question with you editing it every 20 seconds.

Your latest problem appears to be this

<h1 id=="myMessage"></h1>

It should be

<h1 id="myMessage"></h1>

Demo here - http://jsfiddle.net/3Xzka/


the script should be in the body tag or between head tags...


Have you ensured that there is only one element on the page called myMessage? Having more than one will cause getElementById() to fail.


Probably, because you are identifying your code as "javescript" rather than "javascript".

Edit: This reply looks silly now but it answers the original question (before being edited). I was curious and actually tested it: none of my browsers (Opera, Firefox, Chrome, Internet Explorer... not even IE6) handle a <script> tag as JavaScript code if it's tagged as something else, such as:

<script type="text/javescript"></script>
                      ^

JavaScript is the default scripting engine, so this does work:

<script></script>

But the type attribute is obeyed when present.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜