why the javascript code doesn't work? [closed]
<!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.
精彩评论