扩展jQuery [英] Extending jQuery
问题描述
我正在尝试向使用jQuery
的网站添加一些现有功能(我想让draggable
功能与live()
一起使用).
I am trying to add some existing functionality to a site that uses jQuery
(I want to get the draggable
feature working with live()
).
我找到了一些实现此功能的代码,但是我无法使其正常工作.
I found a bit of code which does it but I am having trouble getting it to work.
(function ($) {
$.fn.liveDraggable = function (opts) {
this.live("mouseover", function() {
if (!$(this).data("init")) {
$(this).data("init", true).draggable(opts);
}
});
};
}(jQuery));
我在加载jQuery
和jQuery UI
之后并且实际上未对其进行任何操作之前添加了代码,但是,当我尝试使用它时,出现以下错误:
I added the code after I have loaded jQuery
and jQuery UI
and before I actually do anything with it, however, when I try to use it, I get the following error:
$('.myclass').liveDraggable({}) is undefined.
任何人都可以帮忙吗?
推荐答案
您的代码可以正常工作. $('.myclass').liveDraggable({})
将返回undefined
,因为没有return语句.通常,您要return this
以便可以链接呼叫.
Your code works fine. $('.myclass').liveDraggable({})
is going to return undefined
because there is no return statement. Usually, you want to return this
so you can chain calls.
(function ($) {
$.fn.liveDraggable = function (opts) {
return this.live("mouseover", function() {
if (!$(this).data("init")) {
$(this).data("init", true).draggable(opts);
}
});
};
}(jQuery));
现在您可以将其与其他jQuery方法链接起来.
Now you can chain this with other jQuery methods.
$('.myclass').liveDraggable({}).live('click', function(){
// code
});
这篇关于扩展jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!