簡體   English   中英

如何在外部JS腳本完成運行后運行函數

[英]How to run a function after an external JS script finishes running

我試圖了解如何確保我的代碼在外部JS文件運行完畢后運行。

如果你需要一個特定的實現,你可以看看這個SO問題,我試圖幫助回答: 如何獲得最后一個表行,無論排序?

TDLRbootstrap-sortable.js中的腳本運行表排序。 此表排序完成后; 我想這樣做,以便我可以運行一個片段,它將一個簡單的CSS類添加到新排序表中的最后一個元素。 通過此JQuery代碼段可以輕松實現類的添加:

var lastRow = $(this).closest("table").find("tbody tr:last");

if(!lastRow.hasClass("dropup")){
    // Removing dropup class from the row which owned it
    $(this).closest("table").find("tbody tr.dropup").removeClass("dropup");
    // Adding dropup class to the current last row
    lastRow.addClass("dropup");
}

我想知道:

  1. 外部腳本運行完成后是否可以運行我的腳本?
  2. 如果沒有,你能解釋一下原因嗎?
  3. 我已經考慮修改bootstrap-sortable.js來添加我的腳本,這是最好的推薦方法嗎?

獎金回合! (只有當你覺得自己需要挑戰時)。

除了使用bootstrap-sortable.js作為鏈接問題之外,還有一個更好的,自己動手的解決方案來排序表嗎?

感謝大家!

我要感謝Dave Newton帶領我回答這個問題很簡單。

的jsfiddle

$(document).ready(function() {
  $("#MyTable").on('sorted', function(){
    var lastRow = $("#MyTable").find("tbody tr:last");
      // Removing dropup class from the row which owned it
      $("#MyTable").find("tbody tr.dropup").removeClass("dropup");
      // Adding dropup class to the current last row
      lastRow.addClass("dropup");
  });
});

這很棒,簡單,輕巧,也堅持鏈接的問題。 謝謝戴夫!

暫無
暫無

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

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