简体   繁体   English

如何使用 JS/jQuery 将一页中的多个表转换为 UL 列表?

[英]How to convert multiple tables in one page into UL lists with JS / jQuery?

I would like to convert multiple tables on one page into UL lists.我想将一页上的多个表格转换为 UL 列表。

For each table I have created a numbered class, so as not to find the contents of one table inside another.对于每个表,我都创建了一个编号的类,以免在另一个表中找到一个表的内容。

This is because JS duplicates me the contents of the variables.这是因为JS 复制了我变量的内容

What I would like to achieve is to convert all the tables in UL list, referring to them with a common class, without having to create a numbering.我想要实现的是转换 UL 列表中的所有表,用一个公共类引用它们,而不必创建编号。

So I can create multiple tables, without having the counter limit to have to increment each time, if I want to use multiple tables on the same page.因此,如果我想在同一页面上使用多个表,我可以创建多个表,而不必每次都增加计数器限制。

Thanks in advance!提前致谢!

This is what I tried to do:这就是我试图做的:

 (function() { /*Counter by class numbering so as not to have duplicate table content in others. This is the limiting factor: I can create 3 tables, if I want more, I have to increase it every time*/ var tableInPage = 3; for (let i = 0; i <= tableInPage; i++) { var ul = $('<ul>'); $('.table-list-' + tableInPage[i] + ' ' + 'table tr').each(function() { var li = $('<li>') $('th, td', this).each(function() { var span = $('<span>').html(this.innerHTML); i.append(span); }); ul.append(li); }) $('.table-list-' + tableInPage[i] + ' ' + 'table').replaceWith(ul); } })();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Table 1--> <div class='table-list-1'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div> <!-- Table 2--> <div class='table-list-2'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div>

Here you go.干得好。 JSFiddle Working Link JSFiddle 工作链接

HTML HTML

 <!-- Table 1-->
 <div class='table-list'>
    <table>
      <tr>
          <td>A</td>
          <td>A</td>
      </tr>
     </table>
 </div>

<!-- Table 2-->
<div class='table-list'>
    <table>
      <tr>
          <td>A</td>
          <td>A</td>
      </tr>
     </table>
 </div>
 <!-- Table 3-->
 <div class='table-list'>
    <table>
      <tr>
          <td>A</td>
          <td>A</td>
      </tr>
     </table>
 </div>
 
 and so on .....

JS/JQ JS/JQ

(function () {

/*you don't have to give numbering class to each table. Just give all of them same class and the code will do the rest*/

   $(".table-list").each(function(){
   
    var ul = $('<ul>');
                  
        $('table tr', this).each(function () {
        
            $('th, td', this).each(function () {
              var li = $('<li>')
              var span = $('<span>').html(this.innerHTML);
              li.append(span);
              ul.append(li);
           });
 
         });
         
         $('table', this).replaceWith(ul);
   })

  })();

I've rewritten your code a bit, so now you should not have to change any jQuery if you add another table.我已经稍微重写了您的代码,所以现在如果您添加另一个表,您应该不必更改任何jQuery

(function() {

  $('div[class^="table-list"]').each(function() {

    var ul = $('<ul>');

    $('table tr',this).each(function() {
      var li = $('<li>')
      $('th, td', this).each(function() {
        var span = $('<span>').html(this.innerHTML);
        li.append(span);
      });
      ul.append(li);
    })

    $('table tr',this).replaceWith(ul);
  })
})();

Demo演示

 (function() { $('div[class^="table-list"]').each(function() { var ul = $('<ul>'); $('table tr',this).each(function() { var li = $('<li>') $('th, td', this).each(function() { var span = $('<span>').html(this.innerHTML); li.append(span); }); ul.append(li); }) $('table tr',this).replaceWith(ul); }) })();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Table 1--> <div class='table-list-1'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div> <!-- Table 2--> <div class='table-list-2'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div>

You can just walk this inside out for each element and replace simply with the contained HTML.您可以为每个元素将其从内到外处理,并简单地替换为包含的 HTML。

 (function() { $('div[class^=table-list-]').each(function() { $(this).find('th, td').each(function() { $(this).replaceWith($('<span>').html(this.innerHTML)); }); $(this).find('tr').each(function() { $(this).replaceWith($('<li>').html(this.innerHTML)) }); $(this).find('tbody').each(function() { $(this).replaceWith($('<ul>').html(this.innerHTML)); }); $(this).find('table').each(function() { $(this).replaceWith(this.innerHTML); }); // unclear the requirement to replace the div with the ul but this does that $(this).replaceWith(this.innerHTML); }); })();
 ul { border: solid red 1px; } li { border: solid lime 1px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Table 1--> <div class='table-list-1'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div> <!-- Table 2--> <div class='table-list-2'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div>

You are almost there.你快到了。 Your code just needs a little touch up and it would work.你的代码只需要一点点修饰就可以了。 Please change:请更换:

//i.append(span) to:
li.append(span)

//and tableInPage[i] to:
(i+1)

DEMO演示

 (function() { /*Counter by class numbering so as not to have duplicate table content in others. This is the limiting factor: I can create 3 tables, if I want more, I have to increase it every time*/ var tableInPage = 3; for (let i = 0; i <= tableInPage; i++) { var ul = $('<ul>'); $('.table-list-' + (i+1) + ' ' + 'table tr').each(function() { var li = $('<li>') $('th, td', this).each(function() { var span = $('<span>').html(this.innerHTML); li.append(span); }); ul.append(li); }) $('.table-list-' + (i+1) + ' ' + 'table').replaceWith(ul); } })();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Table 1--> <div class='table-list-1'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div> <!-- Table 2--> <div class='table-list-2'> <table> <tr> <td>A</td> <td>A</td> </tr> </table> </div>

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

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