2017-08-09 69 views
0

我有腳本insertAfter超過一個格

.on('finish.countdown', function(event) { 
     $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
    }) .insertAfter('#cartDefaultHeading'); 

的以下部分我想相同的輸出到後跨越幾個不同的頁面的多個格出現。如果我更改div名稱,它會按預期顯示在新位置。 但是,如果我試圖讓它出現在多個我沒有得到任何輸出。

我嘗試以下

.on('finish.countdown', function(event) { 
     $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
    }) .insertAfter('#cartDefaultHeading').insertAfter('#checkoutOrderHeading'); 

.on('finish.countdown', function(event) { 
    $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
}) .insertAfter('#cartDefaultHeading'), .insertAfter('#checkoutOrderHeading') ; 

甚至

.on('finish.countdown', function(event) { 
    $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
}) .insertAfter('#cartDefaultHeading'); 
    $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
}) .insertAfter('#checkoutOrderHeading'); 

無工作。每當我嘗試在多個div上獲得它時,我都無法獲得它。 實現此目標的正確語法是什麼?

完整腳本如下所示:

<script> 
jQuery(function($){ 
if (($('#cartDefaultHeading').length || $('#checkoutOrderHeading').length) && !$('.checkout_dispatch_timer').length) { 
    var $this = $('<div class="checkout_dispatch_timer <?php echo $dispatch_countdown_timer_style; ?>"></div>'); 
    <?php if ($dispatch_countdown_timer_disable === false) { ?> 
    var finalDate = moment.tz('<?php echo $dispatch_countdown_timer_date_array[4]; ?>', '<?php echo date_default_timezone_get(); ?>'); 
    $this.countdown(finalDate.toDate()) 
    .on('update.countdown', function(event) { 
     var format = '%Mm'; 

     if (event.offset.totalDays > 0) { 
      format = '%Dd:%Hh:' + format; 
     } else if (event.offset.totalHours > 0) { 
      format = '%Hh:' + format; 
     } else { 
      format = format + ':%Ss'; 
     } 

     dispatch_countdown_timer_text = "<?php echo $dispatch_countdown_timer_text; ?>"; 
     dispatch_countdown_timer_text = dispatch_countdown_timer_text.replace('%s', event.strftime(format)); 

     $(this).text(dispatch_countdown_timer_text); 

    }) 
    .on('finish.countdown', function(event) { 
     $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
    }) 
    .insertAfter('#cartDefaultHeading'); 
<?php } else { ?> 
    $this.text("<?php echo $dispatch_countdown_timer_text; ?>").insertAfter('#checkoutOrderHeading'); 
<?php } ?> 
} 
}); 
</script> 
+0

你想插入什麼?被插入的元素是'.on()'之前的元素,而不是您正在回顯的文本。 – Barmar

+0

@Barmar它將根據數據庫中設置的值插入由moment.js計算出的時間。它還根據剩餘的瓦片數量構建特定的消息。 var $ dispatch_countdown_timer_text中的消息內容 –

+0

寫入它的方式,insertAfter在加載頁面時發生,而不是在事件發生時發生。那是你要的嗎? – Barmar

回答

1

不能使用.insertAfter()多次,因爲每個人會從以前的位置嘗試下一個後,將其插入之前刪除的元素。使用.after

$('#cartDefaultHeading, #checkoutOrderHeading').after(
 
    $this.countdown(finalDate.toDate()) 
 
    .on('update.countdown', function(event) { 
 
    var format = '%Mm'; 
 

 
    if (event.offset.totalDays > 0) { 
 
     format = '%Dd:%Hh:' + format; 
 
    } else if (event.offset.totalHours > 0) { 
 
     format = '%Hh:' + format; 
 
    } else { 
 
     format = format + ':%Ss'; 
 
    } 
 

 
    dispatch_countdown_timer_text = "<?php echo $dispatch_countdown_timer_text; ?>"; 
 
    dispatch_countdown_timer_text = dispatch_countdown_timer_text.replace('%s', event.strftime(format)); 
 

 
    $(this).text(dispatch_countdown_timer_text); 
 

 
    }) 
 
    .on('finish.countdown', function(event) { 
 
    $(this).text("<?php echo TEXT_SHIPPING_BARELY_PAST_DISPATCH; ?>").parent().addClass('disabled'); 
 
    }) 
 
);

這假定任何頁面僅有的#cartDefaultHeading#checkoutOrderHeading一個,因爲你不能插入在多個地方(.after.insertAfter相同的元素不進行復印,它移動元素本身)。

+0

導致任何一個div都沒有輸出。我編輯了原始問題以顯示整個腳本,但排除了也在那裏的php代碼。 –

+0

更新了答案 – Barmar

+0

完美適用於主計時器事件。 我假設我將有同樣的問題,添加本節末尾代碼輸出的過期消息: <?php} else {?> $ this.text(「<?php echo $ dispatch_countdown_timer_text;?>「)。insertAfter('#checkoutOrderHeading');

0

根據所述的文檔,可以傳遞元件的功能的陣列。 來源:http://api.jquery.com/insertafter/

`insertAfter(['#cartDefaultHeading','#checkoutOrderHeading']); 
+0

剛剛嘗試過,並且當我得到div的背景顏色時,它應該要添加,#cartDefaultHeading沒有文本內容,而#checkoutOrderHeading會。 如果我將該命令切換爲insertAfter(['#checkoutOrderHeading','#cartDefaultHeading'])頁面,輸出顯示在更改上。因此,看起來文本內容僅適用於insertAfter命令中的最後一個div,即使div設置爲兩者。 –