【问题标题】:Jquery .removeClass before append fails on <tr>在 <tr> 上追加失败之前的 Jquery .removeClass
【发布时间】:2016-12-24 15:44:44
【问题描述】:

见 jsfiddle

https://jsfiddle.net/codenoob/3v3mfndg/1/

我有一张隐藏的桌子。我将其设置为模板以克隆更多行。

但是我想在追加新行时删除隐藏类但不能。

它可以在&lt;tr&gt;之后的所有元素上删除Class

通过从&lt;tr&gt; 中删除隐藏类来测试它并运行代码。

我做错了什么?

如果你不能使用 jsfiddle 这里是代码

HTML

<table id="cloneinhere">
  <tbody>
    <tr id="clone" class="hidden">
      <td>
      <text id="redtext" class="red">hi</text>
      </td>
    </tr>
  </tbody>
</table>
<button id="clonebtn">
  add row
</button>

jquery

var original = $('#clone'); //get html for the sample row

$('#clonebtn').click(function() {
var clone = original.clone();
  clone.find('#clone').removeClass("hidden");
  clone.find('#redtext').removeClass("red");
  $('#cloneinhere > tbody:last').append(clone);
});

CSS

.hidden {
  display: none;
}

.red{
  color: red;
}

【问题讨论】:

  • 有一个显示html、jquery和css的jsfiddle链接
  • 对,但代码不在在您的问题中,它在其他地方可能会或可能无法与您的问题一起访问.

标签: jquery append attr removeclass


【解决方案1】:

检查以下解决方案,将为您解决

$(function() {
var original = $('#clone'); //get html for the sample row

$('#clonebtn').click(function() {
var clone = original.clone();
 clone.removeClass("hidden");
   clone.find('#redtext').removeClass("red");
  $('#cloneinhere > tbody:last').append(clone);
});
});
.hidden {
  display: none;
}

.red{
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="cloneinhere">
  <tbody>
    <tr id="clone" class="hidden">
      <td>
      <text id="redtext" class="red">hi</text>
      </td>
    </tr>
  </tbody>
</table>
<button id="clonebtn">
  add row
</button>

【讨论】:

  • 谢谢,这有效。时间允许时会接受的
猜你喜欢
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多