[英]Add multiple new attributes by checking the existing ones
I am working on a reporting application that pulls info from different servers and displays them in a specific format. 我正在开发一个报表应用程序,该应用程序从不同的服务器提取信息并以特定格式显示它们。 I am also making this completely responsive so the tables that I get look something like this:
我还使它完全响应,所以我得到的表看起来像这样:
<table>
<tr>
<td width="30%">Date</td>
<td width="40%">Description</td>
<td width="17%">Result</td>
<td width="15%">Range</td>
<td width="8%">Comments</td>
</tr>
</table>
I want to know how I could add data-label to each depending on what width they have. 我想知道如何根据它们的宽度为每个标签添加数据标签。
like 喜欢
<td width="30%" data-label="Date">Date</td>
I don't actually need the date field so I have hidden that entire field with CSS its just the description, result, range and comments. 我实际上不需要日期字段,因此我已经用CSS隐藏了整个字段,只是说明,结果,范围和注释。
Pulling a list of all td
elements in the document, and applying the appropriate labels when the widths-in-question are seen: 拉出文档中所有
td
元素的列表,并在看到宽度问题时应用适当的标签:
var tds = document.getElementsByTagName('td'); for ( var i = 0; i < tds.length; ++i ) { var td = tds[i]; var label = null; switch (td.getAttribute('width')) { case '30%': label = 'Date'; break; case '40%': label = 'Description'; break; case '17%': label = 'Result'; break; case '15%': label = 'Range'; break; case '8%': label = 'Comments'; break; } if (label) { td.setAttribute('data-label', label); } }
td[data-label=Date] { color: red; } td[data-label=Description] { color: green; } td[data-label=Result] { color: purple; } td[data-label=Range] { color: blue; } td[data-label=Comments] { font-style: italic; }
<table> <tr> <td width="30%">Date</td> <td width="40%">Description</td> <td width="17%">Result</td> <td width="15%">Range</td> <td width="8%">Comments</td> </tr> <tr> <td width="30%">Blah</td> <td width="40%">Blah</td> <td width="17%">Blah</td> <td width="15%">Blah</td> <td width="8%">Blah</td> </tr> </table>
You can try something along these lines: 您可以尝试以下方法:
One thing I'd suggest, though, is to use 我建议的一件事是使用
<td style="width:30%;">Date</td>
instead of the width attribute. 而不是width属性。 http://www.w3schools.com/tags/att_td_width.asp
http://www.w3schools.com/tags/att_td_width.asp
The width attribute is not HTML5 compliant. width属性不符合HTML5。
window.onload = function(){ var tds = document.querySelectorAll('#mytable td'); for(var i = 0; i < tds.length; i++){ tds[i].setAttribute('data-width', tds[i].getAttribute('width')); } }
<table id="mytable"> <tr> <td width="30%">Date</td> <td width="40%">Description</td> <td width="17%">Result</td> <td width="15%">Range</td> <td width="8%">Comments</td> </tr> </table>
You can do that easily by creating an association on width:data-label 您可以通过在width:data-label上创建关联来轻松实现
(function(){
var assoc = {
30: 'Date',
40: 'Description',
17: 'Result',
15: 'Range',
8: 'Comments'};
var tds = document.getElementsByTagName('td');
for(x=0;x<tds.length;x++){
tds[x].setAttribute('data-label', assoc[parseInt(tds[x].getAttribute('width'))]);
}
();
$(document).ready(function (){
$('#mytable tr td').each(function(){
console.log($(this).attr("data-width",$(this).text());
})
});
plz check it 请检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.