javascript - 检查正在使用的 table

javascript - check what table is in use

我有两个 table,只有一个 javascript 函数与两者关联。我想创建一个案例区分,以便通过 IF 语句 运行 不同的方法,具体取决于选择了哪个 table。

这是我的代码:

<table id="table1" notip="true" class="dataTable no-footer" ...>
  <thead></thead>
  <tbody>
    <tr><td></td></tr>
  </tbody>
</table>

id="table2"也一样 在 javascript 方面,我尝试了以下想法,但它不起作用:

(function($) {
  $.fn.recalculateTabOrder = function() {
    var currentGrid = $(this);
    if (currentGrid === $('#table1')) {
      addTable_1_Item();
    } if (currentGrid === $('#table2')) {
      addTable_2_Item();
    };
  }
})(jQuery);

table1.recalculateTabOrder;
table2.recalculateTabOrder;

就是这个想法。 有没有人知道如何去做,或者有更好的主意?

卢卡

这个条件永远为假,两个jQuery对象永远不会相同

if ( $(this) === $('#table1') ) {

您可以比较底层 DOM 个节点

if ( this === $('#table1').get(0) ) {

甚至只是 ID,或者使用 jQuery 的 is()

if ( $(this).is( $('#table1') ) ) {
// or
if ( this.id === 'table1') {

等等

一起

(function($) {
    $.fn.recalculateTabOrder = function() {
        if (this.id === 'table1') addTable_1_Item();
        if (this.id === 'table2') addTable_2_Item();
    }
})(jQuery);