从html文件中的外部js文件调用Javascript函数

Calling a Javascript function from an external js file in the html file

我无法从HTML文件中的外部javascript文件调用函数。请建议我在哪里犯错...

我已经更新了js文件。

文件1:A.js

1
2
3
4
5
6
7
8
9
10
    (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
function sayHello(){
    console.log("hello");
}





},{}]},{},[1]);

文件2:index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
JS Bin
<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
</style>
</head>
<body>

    <button onclick="sayHello()">click Me</button>
    <script type="text/javascript" src="bundle1.js">
 

  <button type="button">rotate me</button>

</body>
</html>

现在,每当我运行html文件时,单击未找到sayHello()的按钮后,都会出现错误。

命令:browserify A.js -o bundle1.js

请帮助我发现错误!


将脚本类型更改为"text/javascript"或直接省略它,因为HTML5中不再需要它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
JS Bin
<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
</style>
</head>
<body>

    <button onclick="sayHello()">click Me</button>
    <script type="text/javascript" src="A.js">
 

  <button type="button">rotate me</button>

</body>
</html>