簡體   English   中英

准備好文檔時要執行的2種方法的jQuery順序

[英]jQuery order for 2 methods to be executed when document ready

我有這種情況。

$(document).ready(function(){ 
    $.each(elements,do());
});

我想在$.each()完成稱為doOther()新方法之后執行另一個新方法。

我可以在其中設置新方法的地方,例如doOther(); 我有新的$.each里面的新方法只是為了提供信息。

更新

這是我的劇本

$(function(){
    $.each($('.countdown'), function() {
        var _element = '.countdown-'+$(this).attr("id");
        if ($(_element).length > 0) {
            var _expDate = $(_element).attr('data-expiration').split(',');
            _expDate.forEach(function(v,i,a){a[i]=parseInt(a[i]);});
            var _datetime = new Date(_expDate[0],_expDate[1],_expDate[2],_expDate[3],_expDate[4],_expDate[5]);
            init_countdown(_element,_datetime);
        }
    });

    $.each($('.countdown'),function() {
        var ips = $(this).text().split(',');
        $(this).html('<div class="span12">'+ips[0]+'</div>');
    }); 
});

function init_countdown(_element,_datetime){
    $(_element).countdown({
        date: _datetime /*"June 7, 2087 15:03:26"*/
    });
}

似乎$.each($('.countdown')等..被第一個$.each覆蓋,無法理解原因。

如果我了解您的意思,您的代碼應如下所示:

$(document).ready(function(){ 
    $.each(elements, do);
    doOther();
});

如果這不是您的意思,請編輯您的原始問題並添加更多詳細信息。

這應該做到的

$(document).ready( function(){ 
 $.each(elements,do());
 doOther();
} );

在第一個$ .each塊之后實現另一種方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM