Jon*_*n Q 5 html javascript external
我正在尝试使用外部 JavaScript 文件将“Hello World”写入 HTML 页面。
但是由于某种原因它不起作用,我尝试了相同的函数和内联命令并且它起作用了,但在使用外部 JavaScript 文件时不起作用。我在 JS 文件中注释掉的部分是我之前尝试使用的方法。当我从标题和内联运行脚本时,这些行可以工作。谢谢
html文件:
<html>
<head>
</head>
<body>
<p id="external">
<script type="text/javascript" src="hello.js">
externalFunction();
</script>
</p>
<script type="txt/javascript" src="hello.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JavaScript 文件
function externalFunction()
{
var t2 = document.getElementById("external");
t2.innerHTML = "Hello World!!!"
/*document.getElementById("external").innerHTML =
"Hello World!!!";*/
}
Run Code Online (Sandbox Code Playgroud)
通常,您希望将 JavaScript 放在页面底部,因为它通常会减少页面的显示时间。有时您可以在头文件中找到导入的库,但无论哪种方式,您都需要在使用它们之前声明您的函数。
http://www.w3schools.com/js/js_whereto.asp
索引.html
<!DOCTYPE html>
<html>
<head>
<!-- You could put this here and it would still work -->
<!-- But it is good practice to put it at the bottom -->
<!--<script src="hello.js"></script>-->
</head>
<body>
<p id="external">Hi</p>
<!-- This first -->
<script src="hello.js"></script>
<!-- Then you can call it -->
<script type="text/javascript">
externalFunction();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
你好.js
function externalFunction() {
document.getElementById("external").innerHTML = "Hello World!!!";
}
Run Code Online (Sandbox Code Playgroud)
普拉克在这里。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
18377 次 |
| 最近记录: |