如果div未隐藏,请单击任何位置以隐藏
这个问题在这里已经有了答案:
将整个事件处理程序放入一个条件中只会检查第一个页面载入中的条件,并且事件处理程序可能永远不会附加,请尝试如下所示:
$('#theDiv').hide();
$(document).on('click', function(e) {
    if ( $(e.target).closest('#showDivBtn').length ) {
        $("#theDiv").show();
    }else if ( ! $(e.target).closest('#theDiv').length ) {
        $('#theDiv').hide();
    }
});
小提琴
尝试使用
 if( !$('.theDiv' ).is( ':visible' ) ) 
代替
 if ( !$('.theDiv:hidden') ) 
尝试这个
    <script type="text/javascript">
    $('.opendiv').hide();
    $(document).click(function (event) {
        var $target = $(event.target);
        if ($target.attr('id') == 'addAccordion') {
            if ($('.opendiv').is(':hidden')) {
                $('.opendiv').show();
            }
            else {
                $('.opendiv').hide();
            }
        }
        else if ($target.closest('.opendiv').length > 0) {
        }
        else {
            $('.opendiv').hide();
        }
    })
</script>
 <div>
    <input id="addAccordion" type="button" value="ADD COMMENT" />
</div>
<div id="rs" class="opendiv">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">
            www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</div>
                        链接地址: http://www.djcxy.com/p/14699.html
                        上一篇: If div is not hidden click anywhere to hide
下一篇: jQuery: Hide element on click anywhere else apart of the element
