jquery - 不是函数错误
这是我的代码:
(function($){
$.fn.pluginbutton = function (options) {
myoptions = $.extend({ left: true });
return this.each(function () {
var focus = false;
if (focus === false) {
this.hover(function () {
this.animate({ backgroundPosition: "0 -30px" }, { duration: 0 });
this.removeClass('VBfocus').addClass('VBHover');
}, function () {
this.animate({ backgroundPosition: "0 0" }, { duration: 0 });
this.removeClass('VBfocus').removeClass('VBHover');
});
}
this.mousedown(function () {
focus = true
this.animate({ backgroundPosition: "0 30px" }, { duration: 0 });
this.addClass('VBfocus').removeClass('VBHover');
}, function () {
focus = false;
this.animate({ backgroundPosition: "0 0" }, { duration: 0 });
this.removeClass('VBfocus').addClass('VBHover');
});
});
}
});
$(document).ready(function () {
$('.smallTabsHeader a').pluginbutton();
});
它给了我一个错误。怎么了?
Here is my code:
(function($){
$.fn.pluginbutton = function (options) {
myoptions = $.extend({ left: true });
return this.each(function () {
var focus = false;
if (focus === false) {
this.hover(function () {
this.animate({ backgroundPosition: "0 -30px" }, { duration: 0 });
this.removeClass('VBfocus').addClass('VBHover');
}, function () {
this.animate({ backgroundPosition: "0 0" }, { duration: 0 });
this.removeClass('VBfocus').removeClass('VBHover');
});
}
this.mousedown(function () {
focus = true
this.animate({ backgroundPosition: "0 30px" }, { duration: 0 });
this.addClass('VBfocus').removeClass('VBHover');
}, function () {
focus = false;
this.animate({ backgroundPosition: "0 0" }, { duration: 0 });
this.removeClass('VBfocus').addClass('VBHover');
});
});
}
});
$(document).ready(function () {
$('.smallTabsHeader a').pluginbutton();
});
It gives me an error. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个问题的“最佳”解决方法是使用匿名函数这样传递 jQuery 对象:
匿名函数看起来像:
这是 JavaScript 与“模块模式”等一起使用时实现(穷人的)“依赖注入”的方法。
所以你的代码看起来像:
当然,您现在可能想对内部代码进行一些更改,但您已经明白了。
This problem is "best" solved by using an anonymous function to pass-in the jQuery object thusly:
The Anonymous Function Looks Like:
This is JavaScript's method of implementing (poor man's) 'Dependency Injection' when used alongside things like the 'Module Pattern'.
So Your Code Would Look Like:
Of course, you might want to make some changes to your internal code now, but you get the idea.
当不同的系统获取 $ 变量时,就会出现问题。您有多个 $ 变量被用作多个库中的对象,从而导致错误。
要解决此问题,请在
(function($){ 之前使用 jQuery.noConflict:
The problem arises when a different system grabs the $ variable. You have multiple $ variables being used as objects from multiple libraries, resulting in the error.
To solve it, use jQuery.noConflict just before your
(function($){
:更改
为
This 是必要的,因为您需要调用您创建的匿名函数
,并注意到它需要一个将在内部用作
$
的参数,因此您需要传递对 jQuery 的引用目的。此外,您需要将所有
this.
更改为$(this).
,第一个除外,其中您需要return this.each
在第一个(您不需要
$()
)中,这是因为在插件主体中,this
持有一个引用到与您的选择器匹配的 jQuery 对象,但任何比它更深的地方,this
引用特定的 DOM 元素,因此需要将其包装在$()
中。完整代码位于 http://jsfiddle.net/gaby/NXESk/
change
to
This is needed because, you need to call the anonymous function that you created with
and notice that it expects an argument that it will use internally as
$
, so you need to pass a reference to the jQuery object.Additionally, you will need to change all the
this.
to$(this).
, except the first one, in which you doreturn this.each
In the first one (where you do not need the
$()
) it is because in the plugin body,this
holds a reference to the jQuery object matching your selector, but anywhere deeper than that,this
refers to the specific DOM element, so you need to wrap it in$()
.Full code at http://jsfiddle.net/gaby/NXESk/
它适用于我的情况:
It works on my case:
将 ASP.Net Webform 原型转换为 MVC 站点时,我收到以下错误:
它在网络表单中运行良好。问题/解决方案是 _Layout.cshtml 中的这一行,
将其注释掉以查看错误是否消失。然后在BundlesConfig中修复它:
When converting an ASP.Net webform prototype to a MVC site I got these errors:
It worked fine in the webforms. The problem/solution was this line in the _Layout.cshtml
Comment it out to see if the errors go away. Then fix it in the BundlesConfig:
我通过重命名我的函数解决了这个问题。
更改
为
完美工作。
I solved it by renaming my function.
Changed
to
Works perfectly.
就我而言,同样的错误有一个更容易修复的方法。基本上我的函数位于一个 .js 文件中,该文件未包含在当前显示的 aspx 中。我所需要的只是包含行。
In my case, the same error had a much easier fix. Basically my function was in a .js file that was not included in the current aspx that was showing. All I needed was the include line.