【问题标题】:how to remove row and add css class when button clicked单击按钮时如何删除行并添加css类
【发布时间】:2017-04-28 07:12:15
【问题描述】:

这是小提琴

https://jsfiddle.net/shaswatatripathy/enq0kfqL/2/

如何编写删除函数以及如何将css类添加到被点击的行。

1.当我点击删除时,点击的行应该被删除

  1. 当我单击行时 - 行应该附加 highlightrow css 类

  2. 还必须检查表是否有任何行并将其放入var中

  3. 如果没有选择任何行,则一次只有一行应该是红色的(单击的行),删除按钮不应该是可见的

    HTML

    col1header col2header

CSS

.visibilityHide {
    visibility: hidden;

}
.highlightRow{
  color:red;
}
table {
    border-collapse: collapse;
}

table, th, td {
    border: 1px solid black;
}

JS 或 Jquery

function add()
{var addRow1;
 var addRow2;
addRow1 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow1" + "</td><td>" + "col2valuerow1" + "</td></tr>";
addRow2 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow2" + "</td><td>" + "col2valuerow2" + "</td></tr>";
                $("#myTableid tbody").append(addRow1);
                 $("#myTableid tbody").append(addRow2);

}


function remove()
{

}

function getdetails(row)
{
$('#removerow').css('visibility', 'visible');

}

【问题讨论】:

  • 我们也可以使用jquery

标签: javascript jquery html css


【解决方案1】:

这是更新后的 javascript 代码。这将满足您的所有要求。

function add()
{
    var addRow1;
    var addRow2;
    addRow1 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow1" + "</td><td>" + "col2valuerow1" + "</td></tr>";
    addRow2 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow2" + "</td><td>" + "col2valuerow2" + "</td></tr>";
    $("#myTableid tbody").append(addRow1);
    $("#myTableid tbody").append(addRow2);

}


function remove()
{
    $(".highlightRow").remove();
    $('#removerow').addClass('visibilityHide');
    $("#dropDown").prop("disabled", $("#myTableid tbody tr").length > 0);
}

function getdetails(row)
{
    $('#removerow').toggleClass('visibilityHide', $("#myTableid tbody tr").length === 0);
    $(".highlightRow").removeClass("highlightRow");
    $(row).addClass("highlightRow");
}

【讨论】:

  • 这太棒了。一个请求,在删除时,请您输入一个逻辑,例如它的最后一行,我必须再次启用下拉菜单 $("#dropDown").prop("disabled", false);
【解决方案2】:

试试这个。如果它会工作

function add(){
  var addRow1;
  var addRow2;
  addRow1 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow1" + "</td><td>" + "col2valuerow1" + "</td></tr>";
  addRow2 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow2" + "</td><td>" + "col2valuerow2" + "</td></tr>";
  $("#myTableid tbody").append(addRow1);
  $("#myTableid tbody").append(addRow2);
}


function remove(){
  $('.removeClass').remove(); //remove clicked row
  if($('table tbody tr').length <=0){
    $('#removerow').hide();
  }
  if($('table tbody tr').length===0) {
    alert('This last child has been removed');
    $("#dropDown").prop("disabled", false);
  }
}

function getdetails(row){
  $('#removerow').show();
  $(row).addClass('removeClass'); //add class on clicked row
}
.highlightRow{
  color:red;
}
table {
  border-collapse: collapse;
}
table, th, td {
  border: 1px solid black;
}
/* styling for added class so that it looks something different when class added */
.removeClass{
  color:red;
}
#removerow {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="myTableid">
<thead>
  <th>
    col1header
  </th>
  <th>
    col2header
  </th>
</thead>
<tbody>
  
</tbody>
</table>

<input type="button" id ="addrows" value="add" onclick="add()" />
<input type="button" id="removerow" value="remove" onclick="remove()" class="visibilityHide" />

检查该行是否是最后一行是这部分代码

if($('table tbody tr').length==0) {
    alert('This last child has been removed');
    $("#dropDown").prop("disabled", false);
}

【讨论】:

  • 点击行时不应该检查最后一行,情况可能就像用户点击了行但没有删除它,仍然会启用下拉菜单。它可以在remove方法上吗,remvoing时,如果它看到它删除了最后一行,它应该启用下拉列表
  • @tripathy 那么你想禁用最后一行的删除并启用下拉菜单吗?
  • 不,就像当最后一行被删除时,只有下拉应该被启用,否则它不应该被启用
  • @tripathy 我编辑了代码,这样如果你删除了最后一行,if 就会被触发
  • @tripathy 太糟糕了,编辑了代码,删除按钮重新出现。
【解决方案3】:

如下所示(您要求的所有解决方案):-

function add(){
  var addRow1;
  var addRow2;
  addRow1 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow1" + "</td><td>" + "col2valuerow1" + "</td></tr>";
  addRow2 = "<tr onclick="+"getdetails(this)"+"><td>" + "col1valuerow2" + "</td><td>" + "col2valuerow2" + "</td></tr>";
  $("#myTableid tbody").append(addRow1);
  $("#myTableid tbody").append(addRow2);
}


function remove(){
  var index = parseInt($('.removeClass').index())+1;
  $('.removeClass').remove(); //remove clicked row
  $('.removed_row').html("<strong>row number "+index+ " removed</strong>");
  if($('table tbody tr').length <=0){
    $('#removerow').hide();
  }
}

function getdetails(row){
  $('#removerow').css('display', 'block');
  $('.removeClass').removeClass('removeClass');
  $(row).addClass('removeClass'); //add class on clicked row
}
.visibilityHide {
  display: none;
}
.highlightRow{
  color:red;
}
table {
  border-collapse: collapse;
}
table, th, td {
  border: 1px solid black;
}
/* styling for added class so that it looks something different when class added */
.removeClass{
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTableid">
<thead>
  <th>
    col1header
  </th>
  <th>
    col2header
  </th>
</thead>
<tbody>
  
</tbody>
</table>

<input type="button" id ="addrows" value="add" onclick="add()" />
<input type="button" id="removerow" value="remove" onclick="remove()" class="visibilityHide" />

<br>
<br>
<br>

<div class="removed_row"></div>

【讨论】:

  • @tripathy 现在检查解决方案。您所问的所有内容都已添加并现在可以使用
  • 如果我删除所有行,然后再次添加 - 删除按钮不会再次出现
【解决方案4】:

您可以在我的更新中查看答案:

function remove()
{
window.selectedRow.remove();
$('#removerow').css('visibility', 'hidden');
}

function getdetails(row)
{
removeHighlights();
window.selectedRow = row;
row.classList.add("highlightRow");
$('#removerow').css('visibility', 'visible');

}

function removeHighlights() {
var elements = document.getElementsByClassName("highlightRow");
while(elements.length > 0){
    elements[0].classList.remove('highlightRow');
}
}

https://jsfiddle.net/g63zpuuz/

【讨论】:

  • awsum ,但有一个问题,它在删除对象时的说法不支持删除方法
  • 对不起,我无法理解你的想法。你能解释更多吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 2021-10-19
  • 2014-08-22
相关资源
最近更新 更多