组合 jQuery 插件文件会产生错误
我正在组合插件文件来减少 HTTP 请求的数量。删除了调试的最小化。
组合的脚本文件是:
test.js:
(function($){
$.fn.extend({
GetNewDiv: function(){
return $('<div>Testing new div</div');
}
});
})(jQuery)
// ;jQuery.noConflict();
(function($){
$.fn.extend({
GetNewSpan: function(){
return $('<span class="test">Testing new div</span>');
}
});
})(jQuery)
使用该文件的 HTML
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
alert('document ready');
});
</script>
</head>
<body>
test message
</body>
在 Firefox 中打开文件会在 Firebug 中显示此错误 在
function ($) {$.fn.extend({GetNewDiv: function () {return $("<div>Testing new div</div");}});}(jQuery) is not a function
file:///C://js/test.js
Line 10
2 个插件之间添加“jQuery.noConflict();”会有所帮助,但随后错误会更改为 $ 不是一个函数
有什么想法是错误的吗?将多个 .js 文件合并为一个文件的正确方法是什么?
谢谢 阿比
I am combining plugin files to reduce the number of HTTP requests. Removed minimization for debugging.
The combined script file is:
test.js:
(function($){
$.fn.extend({
GetNewDiv: function(){
return $('<div>Testing new div</div');
}
});
})(jQuery)
// ;jQuery.noConflict();
(function($){
$.fn.extend({
GetNewSpan: function(){
return $('<span class="test">Testing new div</span>');
}
});
})(jQuery)
HTML that uses this file
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
alert('document ready');
});
</script>
</head>
<body>
test message
</body>
Opening the file in Firefox displays this error in Firebug
function ($) {$.fn.extend({GetNewDiv: function () {return $("<div>Testing new div</div");}});}(jQuery) is not a function
file:///C://js/test.js
Line 10
Adding "jQuery.noConflict(); " between the 2 plugin helps, but then the error changes to
$ is not a function
Any ideas what is wrong? What is the correct way to combine multiple .js files into one file?
Thanks
Abhi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要在文件之间使用分号即可。这些......中的每一个都是一个 JavaScript 表达式片段。如果没有分号,解析器会认为表达式继续。
分号在 JavaScript 中有时是可选的,但当它们不是时,它们就不是。
You just need a semicolon between the files. Each of those ... things is a JavaScript expression fragment. Without a semicolon, the parser thinks the expression continues.
Semicolons are sometimes optional in JavaScript, but when they're not, they're not.