jQuery dataTables - 从单元格中删除HTML元素

时间:2015-06-21 05:14:02

标签: jquery datatables

尝试根据不同的JQuery事件动态编辑单元格。这就是我希望应该对数据做的事情(以更简单的方式将fns组合在一起)

var d = table.cell(rowindex,cellindex).node().remove(".custom-class1");
table.cell(rowindex,cellindex).data(d);

关于单元格的示例数据

<td>
  <span class="custom-class1"></span>
  <span class="custom-class2"></span>
</td>

应将单元格数据替换为

<td>
  <span class="custom-class2"></span>
</td>

提前致谢!

2 个答案:

答案 0 :(得分:2)

设置

for each row

永远不会奏效。 var d = table.cell(rowindex,cellindex).node().remove(".custom-class1"); 返回一个DOM节点,而不是jQuery对象(使用to$()node()),toJQuery()将删除整个 {{ 1}}无论如何。

要删除特定remove(".custom-class1"),您应使用<td><span>也会移除$('span-selector').remove()

我会将内容检索为jQuery实例,对其进行处理并将其放回原处。单击单元格时,以下内容将删除$somethingContainingSpans.remove('.class')

$somethingContainingSpans

演示 - &gt;的 http://jsfiddle.net/ka4v78jd/

注意:使用<span class="custom-class1">class1</span>(复数方法),因为table.on('click', 'td', function() { var $data = table.cells(this).nodes().to$(); //or just -> var $data = $(table.cell(this).data()); $data.find('span').remove(".custom-class1"); table.cell(this).data($data.html()); }); 似乎不适用于cells(this).nodes()to$() - 但结果是相同的。

答案 1 :(得分:0)

您是否希望通过行和列规范或仅仅是类来删除跨度。如果只是它的类,请尝试:

&#13;
&#13;
$('#button').click(function(){
    var className = $('#classHolder').val();
    $('.'+className).remove();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
  <span class="custom-class1">custom-class1</span>
  <span class="custom-class2">custom-class2</span>
</td>
<br><br>
<input type="text" placeholder="Example: custom-class1" id="classHolder" /><button id="button">Remove Class</button>
&#13;
&#13;
&#13;