代码之家  ›  专栏  ›  技术社区  ›  lovespring

当模糊/或单击表外的文档时如何隐藏表

  •  0
  • lovespring  · 技术社区  · 15 年前

    当模糊/或单击表外的文档时如何隐藏表

    3 回复  |  直到 14 年前
        1
  •  1
  •   Nick Craver    15 年前

    event bubbling 对你有利的是:

    $("#tableID").click(function(e) {
      e.stopPropagation();
    });
    $(document).click(function() {
      $("#tableID").hide();
    });
    

    event.stopPropagation() 我们阻止泡沫到达 document 在内部 桌子。当它来自 在别处 默认情况下 ,把桌子藏起来。在“默认情况下”,我的意思是你没有 return false .stopPropagation() 在那些上面 click 事件。

        2
  •  4
  •   James    15 年前

    clickoutside special event 从本·阿尔曼那里?

    用法:

    $('table').bind("clickoutside", function(event){
        $(this).hide();
    });
    

    或者,如果这看起来有点古怪,那就试试这个( ):

    var myTable = $('table');
    $(document).click(function(e) {
        if (e.target !== myTable[0] && !$.contains(myTable[0], e.target)) {
            myTable.hide();
        }
    });
    
        3
  •  0
  •   ajile    15 年前

    表没有模糊,但表内部的元素有:

    $('#yourtableid:visible a').each(function(){
        $(this).bind('blur', function(){
            $('#yourtableid:visible').hide();
        });
    });
    
    $(document).bind('click', function(){
       $('#yourtableid:visible').hide();
    });
    

    这是最好的主意,不是解决办法