外部JS脚本完成运行后如何运行函数 [英] How to run a function after an external JS script finishes running

查看:79
本文介绍了外部JS脚本完成运行后如何运行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何确保在外部JS文件运行完后代码能够运行.

I am trying to understand how to ensure my code runs after an external JS file has finished running.

如果您需要特定的实现,则可以查看这个SO问题,我将为您解答: 如何不管排序如何获取最后一个表格行?

If you need a specific implementation, you can look at this SO question, which I am trying to help answer: How to get the last table row no matter the sort?

TDLR :在 bootstrap-sortable.js 中找到的脚本运行表排序.该表排序完成后;我想要这样做,以便可以运行一个代码段,这会将一个简单的CSS类添加到刚排序的表中的最后一个元素上.该类的添加可以通过以下JQuery片段轻松实现:

TDLR: The script found in bootstrap-sortable.js runs a table sort. After this table sort is complete; I want to make it so that I can run a snippet, which will add a simple CSS class to the last element in the freshly sorted table. The adding of the class can easily be achieved by this JQuery snippet:

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 以向其中添加脚本,这是最好的推荐方法吗?
  1. Is it possible to run my script after the external script is done running?
  2. If not, can you explain why?
  3. I have already considering modifying bootstrap-sortable.js to add my script to it, is this the best recommendable approach?

奖励回合!(仅在您认为需要挑战的情况下).

Bonus round! (only if you feel you need the challenge).

除了对链接的问题使用bootstrap-sortable.js之外,是否有更好的自行解决方案来对表进行排序?

Is there a better, do-it-yourself, solution for sorting the table other than using bootstrap-sortable.js for the linked question?

谢谢大家!

推荐答案

我要感谢 Dave Newton ,让我找到了这个问题的答案,这很简单.

I want to thank Dave Newton for leading me to the answer to this question which is quite simple.

JSFiddle

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");
  });
});

这真棒,简单且轻巧,它还遵循链接的问题.谢谢戴夫!

This is awesome, simple and lightweight, it also adheres to the linked question. Thanks Dave!

这篇关于外部JS脚本完成运行后如何运行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆