简体   繁体   English

如何在表中添加边框样式到在jquery中动态添加行的表?

[英]How to add border style to a table whose rows are added dynamically in jquery?

I am trying to add css to a table which is dynamically created on button click. 我试图将css添加到在单击按钮时动态创建的表中。 the function which is called on click event has below qjuery part which creates dynamic rows 在click事件中调用的函数在qjuery部分下面,该函数创建动态行

$("#employeDetail").append('<tr class="hide1" id="row'+empCurrentIndex+'">' +'<td >'+employerName[empCurrentIndex]+'</td>' +'<td >'+empMobileNo[empCurrentIndex]+'</td>' +'<td >'+emplocation[empCurrentIndex]+'</td>' +'<td >'+empTotNoMonth[empCurrentIndex]+'</td>' +'<td><button class="btn" name="delete" value="Delete" onclick="return deleteEmpRow('+empCurrentIndex+');">Delete</button></td>' +'<td><button class="btn" name="edit" value="Edit" onclick="return editEmpRow('+empCurrentIndex+');">Edit</button></td>' +'</td></tr>').addClass('newRow');

button is under table tag which calls this javascript function to create dynamic rows. 按钮位于表格标记下,该标记调用此javascript函数以创建动态行。 I want to have border to this created rows. 我想对创建的行设置边框。 i tried by .addClass('newRow') where 我尝试通过.addClass('newRow')在哪里

  .newRow{
   border:5px;
   border-color:red;
  }

but it doesn't seem apply. 但这似乎并不适用。 any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

Your problem is that append() does not return the row, but what you have appended it to. 您的问题是append()不会返回行,而是将其追加到的行。 There are several ways around this issue, but the simplest in this instance is to just put the class in the class attribute where you create the row... 解决这个问题有几种方法,但是在这种情况下,最简单的方法是将类放在创建行的类属性中。

$("#employeDetail").append('<tr class="hide1 newRow" id="row'+empCurrentIndex+'">'
        +'<td >'+employerName[empCurrentIndex]+'</td>'
        +'<td >'+empMobileNo[empCurrentIndex]+'</td>'
        +'<td >'+emplocation[empCurrentIndex]+'</td>'
        +'<td >'+empTotNoMonth[empCurrentIndex]+'</td>'
        +'<td><button class="btn" name="delete" value="Delete" onclick="return deleteEmpRow('+empCurrentIndex+');">Delete</button></td>'
        +'<td><button class="btn" name="edit" value="Edit" onclick="return editEmpRow('+empCurrentIndex+');">Edit</button></td>'
        +'</td></tr>');

I'm not sure about your Javascript, but your CSS should be: 我不确定您的Javascript,但您的CSS应该是:

.newRow td {
   border-bottom: 5px solid red;
}

At the moment, you're adding the newRow class to the <table> tag rather than your <td> tags. 目前,您正在将newRow类添加到<table>标签而不是您的<td>标签。 I've also used short hand for the CSS which specifies that you want a "solid" border. 我还在CSS上使用了简写形式,它指定了您想要的“实心”边框。

Thats because you are adding the newRow class to the #employeDetail. 那是因为您要将newRow类添加到#employeDetail中。 I suppose that is the table element ? 我想那是表格元素吗? You need to add the class to the newly created row. 您需要将类添加到新创建的行。

 $('#row'+empCurrentIndex+').addClass('newRow');

Try that. 试试看

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

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