有没有办法让两个HTML ID链接到一个JavaScript函数? [英] Is there way to make two HTML ID's link to one javascript function?

查看:82
本文介绍了有没有办法让两个HTML ID链接到一个JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表,需要有不同的ID或他们不会工作。我使用的第三方数据表代码使用ID来区分浏览器中的不同表。

 < table id =promoclass =display> 
< table id =officialclass =display>

但我需要它们都链接到相同的脚本函数。正如你在下面看到的,我有一个#promo和一个#official,但他们都使用完全相同的代码:

  $(document).ready(function(){
$('#promo')。DataTable({
//在这里做的东西
});

$('#official')。DataTable({
//在这里做的东西
});

有没有办法让两个表链接到脚本中的相同代码?

解决方案 div>


有没有办法让两个表链接到脚本中的相同代码?



<


  $('#promo,#official')。DataTable({
// a TON的东西在这里
});

它使用 CSS组选择器,但你可以使用任何其他匹配这两个元素的选择器。根据你的问题,例如 .D isplay (一个类选择器)也是如此(提供它不符合你没有显示的其他元素):

  $('。display')。DataTable({
//在这里做了一件TON的东西
});


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 the browser.

<table id="promo" class="display">
<table id="official" class="display">

But I need to them to both link to the same script function. As you can see below, I have one for #promo and one for #official but they are both using the exact same code:

$(document).ready(function() {
    $('#promo').DataTable( {
        // does a TON of stuff here
    } );

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

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

解决方案

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

这篇关于有没有办法让两个HTML ID链接到一个JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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