Javascript 如何解决表行内冲突的单击事件?
我使用jQuery使HTML表行在单击时展开。单击任何其他行将折叠以前打开的行(根据需要) HTML:Javascript 如何解决表行内冲突的单击事件?,javascript,jquery,html,Javascript,Jquery,Html,我使用jQuery使HTML表行在单击时展开。单击任何其他行将折叠以前打开的行(根据需要) HTML: <table class="table table-hover"> <tbody> <tr> <td> Click to expand <div class="details hidden" ><a href="#"
<table class="table table-hover">
<tbody>
<tr>
<td>
Click to expand
<div class="details hidden" ><a href="#" class="collapse-row">Close</a></div>
</td>
</tr>
</tbody>
</table>
$('tr:not(a.collapse-row)').click(function(){
console.log("Expand row");
$('.details').addClass("hidden");
$(this).find('.details').removeClass("hidden");
});
$('.collapse-row').click(function(e){
e.preventDefault();
console.log("Collapse row");
$('.details').addClass("hidden");
});
演示:
但是,我还需要一个折叠行的附加链接(或按钮)。是否有解决冲突单击事件的方法?我尝试了一个:not
选择器(如演示中所示)并使用z-index
,但没有成功。您需要使用
这应该起作用:
$('tr').click(function() {
// Expand clicked row
$(this).find('.details').removeClass("hidden");
// Hide all other rows
$(this).siblings().find('.details').addClass("hidden");
})
$('.collapse-row').click(function(e){
e.preventDefault();
// Close to open row
$(this).addClass("hidden");
})
$('tr').click(function() {
// Expand clicked row
$(this).find('.details').removeClass("hidden");
// Hide all other rows
$(this).siblings().find('.details').addClass("hidden");
})
$('.collapse-row').click(function(e){
e.preventDefault();
// Close to open row
$(this).addClass("hidden");
})