外部JavaScript不运行,不写入文档 [英] External JavaScript Not Running, does not write to document

查看:107
本文介绍了外部JavaScript不运行,不写入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用外部JavaScript文件来将Hello World写入HTML页面。



然而由于某些原因,它不起作用,我尝试了相同的函数和内联命令,它的工作原理,但不是当它使用外部JavaScript文件时。我在JS文件中注释的部分是我尝试使用的以前的方法。这些行可以工作,当我从头部运行脚本,并内联。谢谢

Html档案:

 < html> 

< head>
< / head>

< body>

< p id =external>

< script type =text / javascriptsrc =hello.js>
externalFunction();
< / script>
< / p>

< script type =txt / javascriptsrc =hello.js>< / script>

< / body>

< / html>

JavaScript文件

  function externalFunction()
{
var t2 = document.getElementById(external);

t2.innerHTML =Hello World !!!

/*document.getElementById(\"external\").innerHTML =
Hello World !!!; * /

}


解决方案

通常,您希望将JavaScript放在页面的底部,因为它通常会减少页面的显示时间。有时你可以在头文件中找到导入的库,但无论使用哪种方式,都需要在使用它们之前声明你的函数。



http://www.w3schools.com/js/js_whereto.asp



index.html

 <!DOCTYPE html> 
< html>

< head>
<! - 你可以在这里放置它,它仍然可以工作 - >
<! - 但最好把它放在底部 - >
<! - < script src =hello.js>< / script> - >
< / head>

< body>

< p id =external>嗨< / p>

<! - 第一个 - >
< script src =hello.js>< / script>

<! - 那么你可以称之为 - >
< script type =text / javascript>
externalFunction();
< / script>

< / body>

< / html>

hello.js

  function externalFunction(){
document.getElementById(external)。innerHTML =Hello World !!!;
}

Plunker here

希望这会有所帮助。


I'm trying to use an external JavaScript file in order to write "Hello World" into a HTML page.

However for some reason it does not work, I tried the same function and commands inline and it worked, but not when it's using an external JavaScript file. The part I commented out in the JS file was the previous method I was trying to use. Those lines of could worked when I ran the script from the header, and inline. Thanks

Html file:

<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>

JavaScript file

function externalFunction() 
        {
         var t2 = document.getElementById("external");

            t2.innerHTML = "Hello World!!!"

         /*document.getElementById("external").innerHTML = 
         "Hello World!!!";*/

        }

解决方案

In general, you want to place your JavaScript at the bottom of the page because it will normally reduce the display time of your page. You can find libraries imported in the header sometimes, but either way you need to declare your functions before you use them.

http://www.w3schools.com/js/js_whereto.asp

index.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>

hello.js

function externalFunction() {
  document.getElementById("external").innerHTML = "Hello World!!!";
}

Plunker here.

Hope this helps.

这篇关于外部JavaScript不运行,不写入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆