我正在一个网站(使用WordPress构建)上,并且我有一个表格插件,在其中我无法通过简单地使用br
标签或什至插入span
来打断单词或文本,因为表格插件确实可以不允许HTML标签。
我想要实现的是打破一些th
文本。
具体来说,我想这样分割文本:
安扎尔
齐默(Zimmer)
Netto-
wohnfläche
Verkaufs-
preis
瑞士法郎
我想知道如何用它来实现这一目标。
这里的表th
<th data-class="expand" class="wdtheader sort column-block sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Haus: activate to sort column ascending">Haus</th>
<th class="wdtheader sort column-wohnung sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Wohnung: activate to sort column ascending">Wohnung</th>
<th class="wdtheader sort column-anzahlzimmer sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Anzahl Zimmer: activate to sort column ascending">Anzahl Zimmer</th>
<th class="wdtheader sort column-etage sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Etage: activate to sort column ascending">Etage</th>
<th class="wdtheader sort column-nettowohnflache sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Netto-wohnfläche: activate to sort column ascending">Netto-wohnfläche</th>
<th class="wdtheader sort column-sitzplatz sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Aussenfläche: activate to sort column ascending">Aussenfläche</th>
<th class="wdtheader sort column-verkaufspreischf sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Verkaufspreis CHF: activate to sort column ascending">Verkaufspreis CHF</th>
<th class="wdtheader sort column-pdf sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="PDF: activate to sort column ascending">PDF</th>
<th class="wdtheader sort column-anfrage sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Anfrage: activate to sort column ascending">Anfrage</th>
此处的完整表格代码https://jsfiddle.net/0bkgLqx5/
答案 0 :(得分:1)
我假设表格插件为table
标签或thead
或th
的有效父标签分配了ID。
例如:
HTML:
<table id="table">
<th data-class="expand" class="wdtheader sort column-block sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Haus: activate to sort column ascending">Haus</th>
<th class="wdtheader sort column-anzahlzimmer sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Anzahl Zimmer: activate to sort column ascending">Anzahl Zimmer</th>
<th class="wdtheader sort column-nettowohnflache sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Netto-wohnfläche: activate to sort column ascending">Netto-wohnfläche
</table>
JAVASCRIPT:
var th = document.querySelectorAll('#table th');
th.forEach(function(t) {
var text = t.textContent;
var output = text.replace(/[ |-]/g, '<br>');
console.log(output); // check your browser console for output.
t.innerHTML = output;
});
我正在使用JavaScript替换th
标签内容的空格“”和连字符'-'。
替换模式“ [ |-]
”将找到任何空格或破折号,并用br
标记替换。
除了|
“ OR
”之外,您还可以尝试其他条件。
更新后的答案
根据小提琴中OP的代码。
var th = document.querySelectorAll('#table_1 thead th');
th.forEach(function(t) {
var text = t.textContent;
var output = text.replace(/[ |-]/g, '<br>');
t.innerHTML = output;
});
<div id="table_1_wrapper" class="wpDataTables wpDataTablesWrapper">
<div class="dt-buttons"></div>
<div class="clear"></div>
<div class="wdtscroll">
<table id="table_1" class="scroll responsive display nowrap data-t data-t wpDataTable dataTable" style="" data-described-by="table_1_desc" data-wpdatatable_id="4" role="grid">
<thead>
<tr role="row">
<th data-class="expand" class="wdtheader sort column-block sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Haus: activate to sort column ascending">Haus</th>
<th class="wdtheader sort column-wohnung sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Wohnung: activate to sort column ascending">Wohnung</th>
<th class="wdtheader sort column-anzahlzimmer sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Anzahl Zimmer: activate to sort column ascending">Anzahl Zimmer</th>
<th class="wdtheader sort column-etage sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Etage: activate to sort column ascending">Etage</th>
<th class="wdtheader sort column-nettowohnflache sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Netto-wohnfläche: activate to sort column ascending">Netto-wohnfläche</th>
<th class="wdtheader sort column-sitzplatz sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Aussenfläche: activate to sort column ascending">Aussenfläche</th>
<th class="wdtheader sort column-verkaufspreischf sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Verkaufspreis CHF: activate to sort column ascending">Verkaufspreis CHF</th>
<th class="wdtheader sort column-pdf sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="PDF: activate to sort column ascending">PDF</th>
<th class="wdtheader sort column-anfrage sorting" style="" tabindex="0" aria-controls="table_1" rowspan="1" colspan="1" aria-label="Anfrage: activate to sort column ascending">Anfrage</th>
</tr>
</thead>
<tbody>
<tr role="row" class="odd ani-c-one">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C16 – 0.1</td>
<td class=" column-anzahlzimmer">3½</td>
<td class=" column-etage">EG</td>
<td class=" column-nettowohnflache">ca. 99,0 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 16,5 m<sup>2</sup> + Rasenfläche</td>
<td class=" column-verkaufspreischf">570’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C16-0.1.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C16%20%E2%80%93%200.1"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="even ani-c-two">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C16 – 0.2</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">EG</td>
<td class=" column-nettowohnflache">ca. 113,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 16,5 m<sup>2</sup> + Rasenfläche</td>
<td class=" column-verkaufspreischf">665’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C16-0.2.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C16%20%E2%80%93%200.2"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="odd ani-c-three">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C16 – 1.1</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">OG</td>
<td class=" column-nettowohnflache">ca. 113,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 15,5 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">630’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C16-1.1.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C16%20%E2%80%93%201.1"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="even ani-c-four">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C16 – 1.2</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">OG</td>
<td class=" column-nettowohnflache">ca. 113,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 15,5 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">650’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C16-1.2.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.comh?subject=C16%20%E2%80%93%201.2"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="odd ani-c-five">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C16 – 2.1</td>
<td class=" column-anzahlzimmer">3½</td>
<td class=" column-etage">Attika</td>
<td class=" column-nettowohnflache">ca. 88,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 35,0 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">620’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C16-2.1.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C16%20%E2%80%93%202.1"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="even ani-c-six">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C18 – 0.1</td>
<td class=" column-anzahlzimmer">3½</td>
<td class=" column-etage">EG</td>
<td class=" column-nettowohnflache">ca. 99,0 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 16,5 m<sup>2</sup> + Rasenfläche</td>
<td class=" column-verkaufspreischf">590’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C18-0.1.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C18%20%E2%80%93%200.1"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="odd ani-c-seven">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C18 – 0.2</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">EG</td>
<td class=" column-nettowohnflache">ca. 114,0 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 16,5 m<sup>2</sup> + Rasenfläche</td>
<td class=" column-verkaufspreischf">645’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C18-0.2.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C18%20%E2%80%93%200.2"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="even ani-c-eight">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C18 – 1.1</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">OG</td>
<td class=" column-nettowohnflache">ca. 112,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 15,5 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">650’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C18-1.1.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.comh?subject=C18%20%E2%80%93%201.1"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="odd ani-c-nine">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C18 – 1.2</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">OG</td>
<td class=" column-nettowohnflache">ca. 113,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 16,0 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">630’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C18-1.2.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C18%20%E2%80%93%201.2"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="even ani-c-ten">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C18 – 2.1</td>
<td class=" column-anzahlzimmer">3½</td>
<td class=" column-etage">Attika</td>
<td class=" column-nettowohnflache">ca. 88,5 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 35,0 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">620’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C18-2.1.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C18%20%E2%80%93%202.1"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
<tr role="row" class="odd ani-c-eleven">
<td class=" column-block"><span class="responsiveExpander"></span>C</td>
<td class=" column-wohnung">C18 – 2.2</td>
<td class=" column-anzahlzimmer">4½</td>
<td class=" column-etage">Attika</td>
<td class=" column-nettowohnflache">ca. 132,0 m<sup>2</sup>
</td>
<td class=" column-sitzplatz">ca. 71,0 m<sup>2</sup>
</td>
<td class=" column-verkaufspreischf">845’000</td>
<td class=" column-pdf">
<a href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C18-2.2.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" width="auto" height="20"></a>
</td>
<td class=" column-anfrage">
<a href="mailto:kirchmatt@gribi.com?subject=C18%20%E2%80%93%202.2"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/Anfrage.png" width="auto" height="15"></a>
</td>
</tr>
</tbody>
<tfoot>
<tr style="display: none">
<td class="wdtheader sort column-block" style="" rowspan="1" colspan="1"></td>
<td class="wdtheader sort column-wohnung" style="" rowspan="1" colspan="1"></td>
<td class="wdtheader sort column-anzahlzimmer" style="" rowspan="1" colspan="1">Anzahl Zimmer</td>
<td class="wdtheader sort column-etage" style="" rowspan="1" colspan="1">Etage</td>
<td class="wdtheader sort column-nettowohnflache" style="" rowspan="1" colspan="1"></td>
<td class="wdtheader sort column-sitzplatz" style="" rowspan="1" colspan="1"></td>
<td class="wdtheader sort column-verkaufspreischf" style="" rowspan="1" colspan="1"></td>
<td class="wdtheader sort column-pdf" style="" rowspan="1" colspan="1"></td>
<td class="wdtheader sort column-anfrage" style="" rowspan="1" colspan="1"></td>
</tr>
</tfoot>
</table>
</div>
<div class="dataTables_paginate paging_full_numbers" id="table_1_paginate" style="display: none;"><a class="paginate_button first disabled" aria-controls="table_1" data-dt-idx="0" tabindex="0" id="table_1_first">First</a><a class="paginate_button previous disabled" aria-controls="table_1" data-dt-idx="1" tabindex="0" id="table_1_previous">Previous</a><span><a class="paginate_button current" aria-controls="table_1" data-dt-idx="2" tabindex="0">1</a></span><a class="paginate_button next disabled" aria-controls="table_1" data-dt-idx="3" tabindex="0" id="table_1_next">Next</a><a class="paginate_button last disabled" aria-controls="table_1" data-dt-idx="4" tabindex="0" id="table_1_last">Last</a></div>
</div>
在加载表格后,使用onload
方法加载JavaScript。您也可以将脚本放在页脚中。
window.onload = function() { // code goes here }
感谢 sarah 提到了onload
方法。