Is there way to make two HTML ID's link to one javascript function?

后端 未结 3 1742
情书的邮戳
情书的邮戳 2021-01-27 06:49

I have two tables that need to have different IDs or they will not work. I\'m using 3rd party data table code that uses the ID to differentiate between the different tables in t

相关标签:
3条回答
  • 2021-01-27 07:28

    Alternatively:

    var $DTs = $(".someSemanticClassForAllIntendedTables"); //for easy reference elsewhere
    $DTs.DataTable({ //options });
    
    0 讨论(0)
  • 2021-01-27 07:30

    Is there a way to make both tables link to the same code in a script?

    Yes:

    $('#promo, #official').DataTable( {
        // does a TON of stuff here
    });
    

    This uses a CSS group selector, but you can use any other selector that matches both elements. Based on your question, for instance, .display (a class selector) does as well (provided it doesn't also match other elements you haven't shown):

    $('.display').DataTable( {
        // does a TON of stuff here
    });
    
    0 讨论(0)
  • 2021-01-27 07:36
    $('.display').each(function(){
        $(this).DataTable({
            // does a TON of stuff here
        });
    });
    

    This way you will not have to add each and every id manually. Just add .display to the elements.

    0 讨论(0)
提交回复
热议问题