组合 jQuery 插件文件会产生错误

发布于 2024-10-15 03:16:32 字数 1189 浏览 13 评论 0原文

我正在组合插件文件来减少 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-10-22 03:16:32

您只需要在文件之间使用分号即可。这些......中的每一个都是一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文