我遇到了一个问题,在拖放功能中实现某些内容。目前可拖动和排序已经实现了,但现在的问题是我正在尝试使用按钮来突出显示选定的行并将其删除。
我有两个表:A类和UI类。我设法突出显示了A类表格中的。然而,在UI类表格中,我无法突出显示。
这是我的 jsfiddle。
非常感谢您的帮助。
我有两个表:A类和UI类。我设法突出显示了A类表格中的。然而,在UI类表格中,我无法突出显示。
这是我的 jsfiddle。
非常感谢您的帮助。
您的代码和CSS存在几个问题:
第一个问题 - 您的test2 CSS选择器仅在class A表格下起作用:
您的代码:
.A tbody tr.test2 td {
background: rgba(20, 111, 186, 0.38);
}
我的修复,要通用:
tbody tr.test2 td {
background: rgba(20, 111, 186, 0.38);
}
$('#diagram tbody tr').click(function(e) {
$('#diagram tbody tr').not(this).removeClass('test2');
$(this).toggleClass('test2');
});
我的代码(适用于所有表格的事件委托):
$('tbody').on("click","tr",function(e) {
$('tbody tr').not(this).removeClass('test2');
$(this).toggleClass('test2');
});
可工作的示例: http://jsfiddle.net/jwb7vy9L/9/
带有删除功能的可工作的示例:
$("#button1").on("click",function(e){
$("table:not(.A) .test2").remove();
});
<table class="A" id="diagram">
<tbody id="sortable2">
<tr class="new-item">
<td>1</td>
</tr>
<tr class="new-item">
<td>2</td>
</tr>
<tr class="new-item">
<td>3</td>
</tr>
</tbody>
</table>
<br><br>
<table class="UI" id="diagram1" >
<tbody id="sortable">
</tbody>
</table>
<br><br>
<button id="button1">Clear selected row in Table class UI</button>
JavaScript
test();
$("#sortable").sortable({
revert: true,
stop: function(event, ui) {
if (ui.item.hasClass("new-item")) {
// This is a new item
}
}
});
$(".new-item").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid",
stop: function( event, ui ) {
test();
},
zIndex: 10000
});
/** Highlight Statement **/
function test(){
$('#diagram tbody tr').unbind('click').bind('click',function(e) {
$('#diagram tbody tr').not(this).removeClass('test2');
$(this).toggleClass('test2');
});
$('#diagram1 tbody tr').unbind('click').bind('click',function(e) {
$('#diagram1 tbody tr').not(this).removeClass('test2');
$(this).toggleClass('test2');
});
}
CSS
table{
border: 1px solid black;
width:200px;
}
tbody tr.test2 td {
background: rgba(20, 111, 186, 0.38);
}
$('tbody tr').not(this)
可以替换为$(this).siblings()
。 - scniro