简体   繁体   English

我无法在.html文档上导入.js文件

[英]I can't import a .js file on a .html document

I have a little trouble: on a server I published a .js file where I declared a function that simplifies the document.write() function: 我有点麻烦:在服务器上,我发布了一个.js文件,在其中声明了一个简化document.write()函数的函数:

function print(id, text){document.getElementById(id).innerHTML = text;}

I already tested this code calling the function print("paragraph", "Hello, world!"); 我已经测试了调用函数print("paragraph", "Hello, world!"); and it works, but I don't know why when I created a new .html file and imported the easyjs_demo.js file and in the <script> tag I called the print function, it didn't work: 并且它可以工作,但是我不知道为什么当我创建一个新的.html文件并导入easyjs_demo.js文件,并在<script>标记中调用了print函数时,它不起作用:

<body>
      <p id="paragraph"></p>
      <script src="easyjs_demo.js">
      print("paragraph", "Hello, world!");
      </script>
</body>

Do you know how can I import the .js file wothout having bugs like this? 您是否知道如何在没有此类错误的情况下导入.js文件? Thank You very much! 非常感谢你!

You can't combine includes and code, try separating Your external file, and the inline code like this: 您无法将包含和代码结合在一起,请尝试分离外部文件和内联代码,如下所示:

<script src="easyjs_demo.js"></script>
<script>
  print("paragraph", "Hello, world!");
</script>

Demo Plunker here . 演示Plunker 在这里


Multiple sources show that if You specify the src attribute, the user agent ignores the inline code: 多个来源显示,如果您指定src属性,则用户代理将忽略内联代码:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM