【问题标题】:Chrome cannot find Javascript function in html documentChrome 在 html 文档中找不到 Javascript 函数
【发布时间】:2020-04-22 09:59:39
【问题描述】:

我有一个 html 文件和一个 js 文件。加载 js 文件后,尝试运行任何 js 方法在 chrome 中都不起作用。它们在 IE 中运行良好。在 chrome 控制台中它说该函数未定义,但在网络选项卡下您可以看到该文件已找到并由 html 加载。

我知道,如果我内联编写 js,它就可以正常工作,并且使用这些函数也可以。代码如下:

文件结构:

html ->索引.html js ->test.js

js: test.js

function myTest(){
    alert("TEST")
}

html: index.html

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="../js/test.js"> </script>
    </head>

    <body>

        <button onmousedown="myTest()">Move Up </button>

    </body>

</html>

【问题讨论】:

  • 您遇到了什么错误?

标签: javascript html google-chrome


【解决方案1】:
<button onmousedown="myTest()">Move Up </button>

很遗憾,Chrome 不再支持 onmousedown 事件。

您可以尝试使用(我认为)每个浏览器都支持的 onclick 事件:

<button onclick="myTest()">Move Up </button>

【讨论】:

  • 您可以在此处阅读更多相关信息:stackoverflow.com/questions/41181372/…
  • 我已尝试将代码更改为 onclick,但仍然无法正常工作,chrome 中出现同样的错误。与 onload 等相同。当我查看加载了哪些文件时,它确实说的是 js 文件,但是当在 chrome 工具中打开时,它只是符号。我的其他项目没有这个,我尝试重新创建文件等,它似乎没有帮助
【解决方案2】:

我已经解决了。问题是 html 文档的编码。你想要 UTF-8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多