【问题标题】:How would you end the chain in jquery, then execute your function, but all while inside a chain function您将如何在 jquery 中结束链,然后执行您的函数,但始终在链函数中
【发布时间】:2011-10-26 17:40:50
【问题描述】:

我知道这是一个奇怪的问题,所以我基本上是在说以下内容。我有这个:

$("#dialog-areyousuremodal").dialog({
    resizable: false,
    height: 120,
    modal: true,
    buttons: {
        "Yes": function() {
            $(this).dialog("close");
            // more stuff coming soon
            },
        "No": function() {
            $(this).dialog("close");
        }
    }
}).end().centerFixed();

<div class="hidden" id="dialog-areyousuremodal" title="Delete Confirmation">
    <p>Are you sure you want to delete this row?</p>
</div>

我已经创建了一个自定义函数来扩展 jquery,如下所示:

(function($){
    $.fn.extend({
        centerFixed: function () {
            return this.each(function() {
            $(this).end();
            $(this).css({position:"fixed"});
            $(this).css("top", (($(window).height() - $(this).outerHeight()) / 2) + "px");
            $(this).css("left", (($(window).width() - $(this).outerWidth()) / 2) + "px");
            });
        }
    });
})(jQuery);

我的目标是替换 .end().centerFixed();只需 .centerFixed();。如您所见,我尝试将 .end() 放入自定义函数中,但这不起作用。我试图避免将 .end() 放在对话框的末尾或将 .centerFixed() 放在新的一行。

对这件事有什么想法吗?

【问题讨论】:

  • 我摸不着头脑,想不出为什么你会想要这样做。你能解释一下为什么你不能使用end(),可能有更简单的解决方案?
  • 如果你问为什么我需要在运行我的函数之前结束链:原因是因为在我更改 css 之前需要创建 jqueryui 的对话框窗口。
  • 如果您问为什么我需要它来工作而不必键入 end() 或转到换行符,那是因为我的队友只是希望它以这种方式工作。 (我知道,这很疯狂,但是嘿......我被投票了)

标签: jquery dialog position modal-dialog


【解决方案1】:

恐怕你根本做不到,当centerFixed() 被调用时,它没有在末尾调用的函数链的概念。它只知道this,它是从前一个链方法中调用的函数返回的任何对象。

由于您在对dialog() 的调用结束时将其链接起来,因此在centerFixed 内部,变量this 将是调用dialog() 的结果的任何对象(我认为这是UI对话对象)。

如果您想确保您的代码仅在创建对话框时运行,首选方法是使用插件的create event

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2021-09-29
    • 2013-07-01
    • 2011-10-01
    • 2014-01-20
    • 2014-10-04
    相关资源
    最近更新 更多