简体   繁体   English

如何在HTML表格中将样式属性添加到列的所有单元格?

[英]How to add style attribute to all cells of a coloumn in a HTML table?

So I have a huge HTML Table, some of which I've inserted here below: 因此,我有一个巨大的HTML表,其中一些是在下面插入的:

<thead>
  <tr class="tableizer-firstrow">
    <th>Name</th>
    <th>Language</th>
    <th>Pages</th>
    <th>Author</th>
    <th>Publisher</th>
    <th>Category</th>
    <th>Class 1</th>
    <th>Class 2</th>
    <th>Class 3</th>
    <th>Class 4</th>
    <th>Class 5</th>
    <th>Class 6</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>Sarvanna Shikshan- Swapna Navhe Hakka!</td>
    <td>Marathi</td>
    <td>64</td>
    <td>Vinaya Deshpande</td>
    <td>Bharat Gyan Vigyan Samuday (BGVS) Maharashtra</td>
    <td>Uncategorized</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Apalya Gavat Aple Arogya</td>
    <td>Marathi</td>
    <td>&nbsp;</td>
    <td>-</td>
    <td>Cehat Pune</td>
    <td>Uncategorized</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</tbody>

I have thousands of rows, but what I want for each row is something like this 我有数千行,但是我想要的每一行都是这样的

<tr>
  <td class="name">Sarvanna Shikshan- Swapna Navhe Hakka!</td>
  <td class="Language">Marathi</td>
  <td class="Pages">64</td>
  <td class="Author">Vinaya Deshpande</td>
  <td class="Publisher">Bharat Gyan Vigyan Samuday (BGVS) Maharashtra</td>
  <td class="Category">Uncategorized</td>
  <td class="Class 1">&nbsp;</td>
  <td class="Class 2">&nbsp;</td>
  <td class="Class 3">&nbsp;</td>
  <td class="Class 4">&nbsp;</td>
  <td class="Class 5">&nbsp;</td>
  <td class="Class 6">&nbsp;</td>
</tr>

Is there any way I can insert this for all of the cells? 有什么办法可以插入所有单元格吗? Maybe a search and replace which acts on every 13th iteration or something like that? 也许搜索并替换会在第13次迭代中起作用,或者类似的事情? There's no way I'll be able to do this manually anyway. 无论如何,我将无法手动完成此操作。 Sorry if it's in the wrong topic, I'm not very familiar with Stackoverflow. 很抱歉,如果主题有误,我对Stackoverflow不太熟悉。

MAKE A BACKUP OF THE HTML CODE AS THIS MIGHT FAIL! 可能会失败,请备份HTML代码!

First open the html in Notepad++. 首先在Notepad ++中打开html。 Open up "Search and replace" in the Search option (Ctrl+F). 在搜索选项(Ctrl + F)中打开“搜索并替换”。 Check the "Regular Expresssion" Option and the "find \\r and \\n" Option. 检查“正则表达式”选项和“查找\\ r和\\ n”选项。

This is the search text: 这是搜索文本:

<tr>(.*?)\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)<td>(.*?)</td>\r\n(.*?)</tr>

Click "replace all" or just go trough step by step by clicking "replace". 单击“全部替换”,或通过单击“替换”一步一步走。

This is what the replace looks like: 这是替换的样子:

<tr>\1\r\n\2<td class=\"name\">\3</td>\r\n\4<td class=\"Language\">\5</td>\r\n\6<td class=\"Pages\">\7</td>\r\n\8<td class=\"Author\">\9</td>\r\n$10<td class=\"Publisher\">$11</td>\r\n$12<td class=\"Category\">$13</td>\r\n$14<td class=\"Class 1\">$15</td>\r\n$16<td class=\"Class 2\">$17</td>\r\n$18<td class=\"Class 3\">$19</td>\r\n$20<td class=\"Class 4\">$21</td>\r\n$22<td class=\"Class 5\">$23</td>\r\n$24<td class=\"Class 6\">$25</td>\r\n$26</tr>

I'm not the best programmer so ya. 我不是最好的程序员。 I hope this works for all as I just tested it on the small snippet you gave us. 我希望这对所有人都有效,因为我刚刚在您提供给我们的小片段中对其进行了测试。

Here is a javascript solution: You can always run it in your browser, then copy/paste the output from the DevTool. 这是一个javascript解决方案:您始终可以在浏览器中运行它,然后从DevTool复制/粘贴输出。

Note: Class attribute cannot contain space. 注意:类属性不能包含空格。 If it does, the element will have 2 class. 如果是这样,则该元素将具有2类。 For example: Class and 1 not just Class 1 例如: Class1不只是Class 1

 var cls = ['name', 'Language', 'Pages', 'Author', 'Publisher', 'Category', 'Class-1', 'Class-2', 'Class-3', 'Class-4', 'Class-5', 'Class-6']; [].forEach.call(document.querySelectorAll('tbody > tr'), function(row) { [].forEach.call(row.querySelectorAll('td'), function(cell, index) { cell.classList.add(cls[index]); }); }); 
 table { border-collapse:collapse; } td { border:1px solid; } 
 <table> <thead> <tr class="tableizer-firstrow"> <th>Name</th> <th>Language</th> <th>Pages</th> <th>Author</th> <th>Publisher</th> <th>Category</th> <th>Class 1</th> <th>Class 2</th> <th>Class 3</th> <th>Class 4</th> <th>Class 5</th> <th>Class 6</th> </tr> </thead> <tbody> <tr> <td>Sarvanna Shikshan- Swapna Navhe Hakka!</td> <td>Marathi</td> <td>64</td> <td>Vinaya Deshpande</td> <td>Bharat Gyan Vigyan Samuday (BGVS) Maharashtra</td> <td>Uncategorized</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>Apalya Gavat Aple Arogya</td> <td>Marathi</td> <td>&nbsp;</td> <td>-</td> <td>Cehat Pune</td> <td>Uncategorized</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </tbody> </table> 

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

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