gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:06:29 25 4
gpt4 key购买 nike

我遇到了一点麻烦:在服务器上我发布了一个 .js 文件,其中声明了一个简化 document.write() 的函数。功能:

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

I already tested this code calling the function 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:

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

你知道如何导入 .js 文件而不出现这样的错误吗?非常感谢!

最佳答案

您不能合并包含文件和代码,请尝试将外部文件和内联代码分开,如下所示:

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

演示插件 here

<小时/>多个来源表明,如果您指定 src 属性,用户代理会忽略内联代码:

关于javascript - 我无法在 .html 文档上导入 .js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46154474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com