简体   繁体   English

通过检查现有属性来添加多个新属性

[英]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.

相关问题 如何向 JavaScript 中的现有键值添加新键值? - How can I add new key values to existing ones in JavaScript? 向现有 js 对象添加多个属性 - Add multiple attributes to an existing js object 无法向现有模型sails.js添加新属性 - Can't add new attributes to existing model sails.js 如何在React中动态地在一些现有元素之间添加新元素? - How do I add new elements in between some existing ones dynamically in React? JavaScript - 将新字段添加到现有字段 - JavaScript - Adding the new fields to existing ones Javascript多次拖放文件 - 将新文件添加到以前的文件而不是替换 - Javascript Drag n Drop files multiple times - add new files to previous ones instead of substituting 删除多个选择选项并替换为新选项 - Removing multiple select options and replace with new ones 我正在使用 select2 多个添加....我在 select2 下拉列表中有一个类别列表,其中包含添加新类别的选项 - I am using select2 multiple add....i have a list of categories in select2 dropdown with an option to add new ones 确定是创建新对象还是更新现有对象 - Determining whether to create new Objects or update existing ones 删除现有的类别和频道并创建新的(discord.js)? - Delete existing categories and channels and make new ones(discord.js)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM