-5

I was trying to sun some javascript on my html page but it doesn't not run at all. I tried to just change the words in a <p> tag but nothing happened. I know javascript is enabled on my browser. Here is my code, I can't seem to find anything wrong with it.

<!DOCTYPE html>
<html>
<head>

</head>
<body>
    <h1> Eat Page</h1>
    <p id="test">Hi</p>
    <select id="CusineList"></select>

    <script type="text/javascript">
        document.getElementIdBy("test").innerHTML = "Who";
    </script>

    <p> When </p>
</body>

</html>

Note- I am running this file off a folder on my desktop

4

5 回答 5

5
document.getElementById("test").innerHTML = "Who";

NOT

document.getElementIdBy("test").innerHTML = "Who";
于 2012-06-30T04:09:51.173 回答
3

Javascript has no function getElementIdBy, you have spelled it wrong. You should use document.getElementById.

Wrong sytnax

  document.getElementIdBy("test").innerHTML = "Who";

Correct syntax

  document.getElementById("test").innerHTML = "Who";

You script block should be

<script type="text/javascript">
        document.getElementById("test").innerHTML = "Who";
</script>

Note: As you are concerned about that you are running file from desktop its absolutely correct in your case.

On more advice put the script block just before the closing tag of body instead of putting it between the HTML. It makes the code bit messy.

于 2012-06-30T04:09:41.073 回答
0

这是正确的代码,

document.getElementById("test").innerHTML = "Who";

您输入错误:

document.getElementIdBy("test").innerHTML = "Who";

检查此链接:http: //jsfiddle.net/Q3eMV/

于 2012-06-30T04:13:45.973 回答
0

呃,应该是getElementById,不是getElementIdBy。下次,在询问之前查看MDN 文档。

document.getElementById("test").innerHTML = "Who";
于 2012-06-30T04:15:02.067 回答
0

您应该注意文档加载,因为您是在没有任何事件的情况下直接加载 javascript。如果您先加载脚本然后再加载 div,即使您确实犯了任何语法错误,您也肯定会遇到问题。

它应该在所有文档都加载后加载。

如果您使用的是 jQuery,则可以在$.ready()函数内运行脚本。

于 2012-12-12T12:14:22.970 回答