我无法从HTML文件中的外部javascript文件调用函数。请建议我在哪里犯错...
我已经更新了js文件。
文件1:A.js
(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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<div>
<button onclick="sayHello()">click Me</button>
<script type="text/javascript" src="bundle1.js"></script>
</div>
<div>
<button type="button">rotate me</button>
</div>
</body>
</html>
现在,每当我运行html文件时,单击未找到sayHello()的按钮后,都会出现错误。
命令:browserify A.js -o bundle1.js
请帮助我发现错误!
答案 0 :(得分:1)
将脚本类型更改为"text/javascript"
或由于HTML5不再需要而将其省略。
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<div>
<button onclick="sayHello()">click Me</button>
<script type="text/javascript" src="A.js"></script>
</div>
<div>
<button type="button">rotate me</button>
</div>
</body>
</html>