简体   繁体   English

jQuery onPropagation不适用于.getJSON()

[英]jQuery onPropagation not working with .getJSON()

I have a table. 我有桌子 When I click on a cell, it creates rows below it that show the next-level-down detail, using a .click() event listener for that class. 当我单击一个单元格时,它将使用该类的.click()事件侦听器在其下方创建显示下一级细节的行。 It does this with a .getJSON() call, that I then use to populate the rows that follow. 它使用.getJSON()调用来完成此操作,然后我用它来填充随后的行。 I added the following .newtable class to each new (sub-detail) row. 我将以下.newtable类添加到每个新的(子详细信息)行。 At the bottom of my page I have the following, so if you click anywhere on the page it closes these sub-detail rows: 在页面底部,我有以下内容,因此,如果您单击页面上的任意位置,它将关闭这些子详细信息行:

$('html').click(function(){
    $('.newtable').remove();        
});

However, I don't want it to close the sub-detail if the user clicks in one of the newly opened cells, so I tried both of the following with no avail... is it b/c the rows are being added after the page loads or something? 但是,如果用户单击新打开的单元格之一,我不希望它关闭子详细信息,因此我尝试了以下两种方法都无济于事...是在行之后添加行吗?页面加载还是什么?

$('.newtable td').click(function(e){
    e.stopPropagation();
});

and

$(document).ready(function(){
    $("table .newtable").on("click","td",function(e){
        e.stopPropagation();
    });
});

Any help is greatly appreciated! 任何帮助是极大的赞赏!

I think this should work for you without the other two code snippets you're trying. 我认为这应该对您有用,而无需您尝试其他两个代码段。

$('html').click(function(event){
    if($(event.target).hasClass('newtable') || $(event.target).parents('tr').first().hasClass('newtable')){
        return;
    } else {
        $('.newtable').remove();        
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM