简体   繁体   English

通用javascript选择器,可在所有html元素上进行交互

[英]universal javascript selector to interact on all html element

I started on that http://jsfiddle.net/DRFBG/ 我从那个http://jsfiddle.net/DRFBG/开始

And if I add tables so mytable1 , mytable2 ,... 如果我添加表,那么mytable1mytable2 ,...

<table id="mytable1" border="1">  
 <tr><th>Column1</th><th>Column2</th><th>Column3</th><th>Column4</th></tr>
 <tr class="data"><td>1st</td><td>1.1</td><td></td><td>1</td></tr>
 <tr class="data"><td>2nd</td><td>2.01</td><td></td><td>2</td></tr>  
 <tr class="data"><td>3rd</td><td>3.001</td><td></td><td>3</td></tr>  
 <tr class="data"><td>4th</td><td>4.01</td><td></td><td>4</td></tr>
</table>

<table id="mytable2" border="1">  
 <tr><th>Column1</th><th>Column2</th><th>Column3</th><th>Column4</th></tr>
 <tr class="data"><td>1st</td><td>1.1</td><td>1</td><td></td></tr>
 <tr class="data"><td>2nd</td><td>2.01</td><td>2</td><td></td></tr>  
 <tr class="data"><td>3rd</td><td>3.001</td><td>3</td><td></td></tr>  
 <tr class="data"><td>4th</td><td>4.01</td><td>4</td><td></td></tr>
</table>

How could I uniform my javascript code for all tables? 我如何统一所有表的JavaScript代码?

I've already tried passing by table[div^=mytable ]*, but the problem is the second selector in the function. 我已经尝试过通过table [div ^ = mytable ] *进行传递,但是问题是该函数中的第二个选择器。

So any ideas please? 有什么想法吗? Thank you? 谢谢? Sorry for my english 对不起我的英语不好

By the way, the code is to remove th with empty td for each table 顺便说一句,代码是为每个表删除空td的th

$('#mytable2 th').each(function(i) {

    var remove = 0;

    var tds = $(this).parents('table').find('tr td:nth-child(' + (i + 1) + ')')
    tds.each(function(j) { if (this.innerHTML == '') remove++; });

    if (remove == ($('#mytable2 tr').length - 1)) {
         $(this).hide();
        tds.hide();
    }
});

One approach is, selecting table s first and get their id and after that, doing the approach of http://jsfiddle.net/DRFBG/ on each of them like the following: 一种方法是,首先选择table s并获取其ID,然后再对其进行http://jsfiddle.net/DRFBG/的处理,如下所示:

$('table').each(function()
{
    var tb_id = $(this).attr('id');
    $('#'+tb_id+' th').each(function(i) {
        var remove = 0;

        var tds = $(this).parents('table').find('tr td:nth-child(' + (i + 1) + ')')
        tds.each(function(j) { if (this.innerHTML == '') remove++; });

        if (remove == ($('#'+tb_id+' tr').length - 1)) {
            $(this).hide();
            tds.hide();
        }
    });
});

Here is the working jsfiddle 这是工作的jsfiddle

To select all on your page you can use "table" selector. 要选择页面上的所有内容,可以使用"table"选择器。

So you'd need to use $('table2 th') instead of $('#mytable2 th') 因此,您需要使用$('table2 th')代替$('#mytable2 th')

One possible solution would be to loop through each column of each table, then check if there are any non-empty cells. 一种可能的解决方案是遍历每个表的每一列,然后检查是否有任何非空单元格。 If there is not, then you can safely remove() all the td and th within that column. 如果没有,则可以安全地remove()该列中的所有tdth

Note that the removal needs to be done last, otherwise it will affect the indexing of the following columns. 请注意,删除操作需要最后完成,否则将影响以下列的索引。 You can do that by simply marking the cells to be removed with a class, and then selecting that class once all loops complete. 您可以通过简单地用类标记要删除的单元格,然后在所有循环完成后选择该类来做到这一点。 Try this: 尝试这个:

 $('table').each(function() { var $table = $(this); var rows = $table.find('tr').length - 1; // -1 to account for the headings $table.find('th').each(function(i, th) { var $empty = $table.find(`td:nth-child(${i + 1}):empty`); if ($empty.length == rows) $empty.add(this).addClass('to-remove'); }) $table.find('.to-remove').remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="mytable1" border="1"> <tr> <th>Column1</th> <th>Column2</th> <th>Column3</th> <th>Column4</th> </tr> <tr class="data"> <td>1st</td> <td>1.1</td> <td></td> <td>1</td> </tr> <tr class="data"> <td>2nd</td> <td>2.01</td> <td></td> <td>2</td> </tr> <tr class="data"> <td>3rd</td> <td>3.001</td> <td></td> <td>3</td> </tr> <tr class="data"> <td>4th</td> <td>4.01</td> <td></td> <td>4</td> </tr> </table> <table id="mytable2" border="1"> <tr> <th>Column1</th> <th>Column2</th> <th>Column3</th> <th>Column4</th> </tr> <tr class="data"> <td>1st</td> <td>1.1</td> <td>1</td> <td></td> </tr> <tr class="data"> <td>2nd</td> <td>2.01</td> <td>2</td> <td></td> </tr> <tr class="data"> <td>3rd</td> <td>3.001</td> <td>3</td> <td></td> </tr> <tr class="data"> <td>4th</td> <td>4.01</td> <td>4</td> <td></td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM