如何在调用js对象的另一个方法后调用另一个方法?

发布于 2024-08-27 08:09:12 字数 417 浏览 3 评论 0原文

我经常在 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 技术交流群。

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

发布评论

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

评论(2

梦醒时光 2024-09-03 08:09:12

你所指的就是所谓的链接。除了延迟用于动画这一事实之外,要记住的关键点是从函数返回 this

如果您想了解特定的 jQuery 函数是如何工作的,请查看 removeClass 的代码:

removeClass: function( value ) {
    if ( jQuery.isFunction(value) ) {
        return this.each(function(i) {
            var self = jQuery(this);
            self.removeClass( value.call(this, i, self.attr("class")) );
        });
    }

    if ( (value && typeof value === "string") || value === undefined ) {
        var classNames = (value || "").split(rspace);

        for ( var i = 0, l = this.length; i < l; i++ ) {
            var elem = this[i];

            if ( elem.nodeType === 1 && elem.className ) {
                if ( value ) {
                    var className = (" " + elem.className + " ").replace(rclass, " ");
                    for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
                        className = className.replace(" " + classNames[c] + " ", " ");
                    }
                    elem.className = jQuery.trim( className );

                } else {
                    elem.className = "";
                }
            }
        }
    }

    return this;
},

注意到最后的 return this; 了吗?这允许您调用 $('#myelement').removeClass('highlight').someOtherFunction(),因为在removeClass 末尾,您仍在对 jQuery 对象本身进行操作。

如果您想检查其他功能,请查看 jQuery 的开发版本(未缩小): http://code.jquery.com/jquery-1.4.2.js

编辑:为了更全面地回答您的问题,您可以编写函数以允许按以下方式链接(在 jQuery 包含之后包含) :

(function($){
    $.fn.extend({
        action1: function() { return this; },
        action2: function() { return this; }
    })(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:

removeClass: function( value ) {
    if ( jQuery.isFunction(value) ) {
        return this.each(function(i) {
            var self = jQuery(this);
            self.removeClass( value.call(this, i, self.attr("class")) );
        });
    }

    if ( (value && typeof value === "string") || value === undefined ) {
        var classNames = (value || "").split(rspace);

        for ( var i = 0, l = this.length; i < l; i++ ) {
            var elem = this[i];

            if ( elem.nodeType === 1 && elem.className ) {
                if ( value ) {
                    var className = (" " + elem.className + " ").replace(rclass, " ");
                    for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
                        className = className.replace(" " + classNames[c] + " ", " ");
                    }
                    elem.className = jQuery.trim( className );

                } else {
                    elem.className = "";
                }
            }
        }
    }

    return this;
},

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):

(function($){
    $.fn.extend({
        action1: function() { return this; },
        action2: function() { return this; }
    })(jQuery);

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.

谁的新欢旧爱 2024-09-03 08:09:12

.delay() 用于动画

当然还有其他方法可以这样做,但是 jQuery 可以以一种很棒的方式链接。也就是说,如果您不愿意,则不必编写一堆匿名函数或自定义回调。 方便链接!

另一方面,如果您确实想自定义动画的回调,也可以!

$('#foo').slideDown(500, function(){
  alert('foo is visible now!');
});

查看 jQuery Effects API 了解更多信息。

查看 JavaScript 的原生 setTimeout() 也有功能。

.delay() is used for animations

Sure 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!

$('#foo').slideDown(500, function(){
  alert('foo is visible now!');
});

Check out the jQuery Effects API for more information.

It might be helpful to look at JavaScript's native setTimeout() function, too.

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