如何在调用js对象的另一个方法后调用另一个方法?
我经常在 jQuery 中看到这样的代码。
$('div').action1().delay(miliseconds).action2();
我可以通过以下代码中的一级操作来实现它。
function $(id) {
var $ = document.getElementById(id);
$.action1 = function() {
};
return $;
}
如何编写方法 delay()
和 action2()
以便我可以这样使用它们?
$('div').action1().delay(miliseconds).action2();
I often saw this code in jQuery.
$('div').action1().delay(miliseconds).action2();
I could realize it in one level action in the following code.
function $(id) {
var $ = document.getElementById(id);
$.action1 = function() {
};
return $;
}
How to write the method delay()
and action2()
so that I could use them this way?
$('div').action1().delay(miliseconds).action2();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所指的就是所谓的链接。除了延迟用于动画这一事实之外,要记住的关键点是从函数返回
this
。如果您想了解特定的 jQuery 函数是如何工作的,请查看
removeClass
的代码:注意到最后的
return this;
了吗?这允许您调用 $('#myelement').removeClass('highlight').someOtherFunction(),因为在removeClass 末尾,您仍在对 jQuery 对象本身进行操作。如果您想检查其他功能,请查看 jQuery 的开发版本(未缩小): http://code.jquery.com/jquery-1.4.2.js
编辑:为了更全面地回答您的问题,您可以编写函数以允许按以下方式链接(在 jQuery 包含之后包含) :
这本质上是创建您自己的插件。您可以在网上找到很多有关如何执行此操作的教程,或者打开您已经看过的插件。
这是一个简单的教程:http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial" queness.com/post/112/a-really-simple-jquery-plugin-tutorial
Edit2:在创建 jQuery 插件方面没有太深入(因为您的问题一般是关于 javascript 的),我只是想提一下如果你正在做一个 jQuery 插件,你会想要这样做:
返回 this.each(function() {});
所以你的函数对选择器选择的所有元素执行。
What you're referring to is called chaining. Aside from the fact that delay is used for animations, the key point to remember is to return
this
from your function.If you'd like to see how specific jQuery functions work, check out the code for
removeClass
:Notice the
return this;
at the end? That's allowing you to call$('#myelement').removeClass('highlight').someOtherFunction()
, because at the end of the removeClass, you're still operating on the jQuery object itself.If you'd like to examine other functions, check out the development version (unminified) of jQuery: http://code.jquery.com/jquery-1.4.2.js
Edit: to more fully answer your question, you can write your functions to allow chaining in the following way (include after your jQuery include):
This is essentially creating your own plugin. You can find a lot of tutorials online for how to do so, or open a plugin you already have an take a look at it.
Here is one simple tutorial: http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial
Edit2: Without getting too far into creating jQuery plugins (since your questions is about javascript in general), I just wanted to mention that if you're doing a jQuery plugin, you'll want to do:
return this.each(function() {});
so your function performs on all elements chosen by the selector.
.delay()
用于动画当然还有其他方法可以这样做,但是 jQuery 可以以一种很棒的方式链接。也就是说,如果您不愿意,则不必编写一堆匿名函数或自定义回调。 方便链接!
另一方面,如果您确实想自定义动画的回调,也可以!
查看 jQuery Effects API 了解更多信息。
查看 JavaScript 的原生
setTimeout()
也有功能。.delay()
is used for animationsSure there's other ways you can do this, but jQuery is chainable in an awesome way. That is, you don't have to write a bunch of anonymous functions or custom callbacks if you don't want to. Convenience in chaining!
On the other hand, if you actually want to customize the callbacks of animations, you can!
Check out the jQuery Effects API for more information.
It might be helpful to look at JavaScript's native
setTimeout()
function, too.