Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决表行内冲突的单击事件?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何解决表行内冲突的单击事件?

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="#"

我使用jQuery使HTML表行在单击时展开。单击任何其他行将折叠以前打开的行(根据需要)

HTML

<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");
})