简体   繁体   English

在表格外单击时隐藏div

[英]Hide div when clicking outside the table

I am quite new to Javascript (intermediate in HTML & CSS), though I am pretty good at working things out on my own by looking up other's examples. 我对Javascript(HTML和CSS的中级语言)很陌生,尽管我擅长通过查找其他示例来自行解决问题。 Unfortunately, I am having significant trouble with this one. 不幸的是,我对此有很大的麻烦。

I have a table displaying 3 links to the viewer. 我有一个显示3个指向查看器的链接的表。 Each link when clicked, slides a hidden div open to the right. 单击每个链接后,会将隐藏的div滑动到右侧。 When one of the other links are clicked, the opened div slides back to being hidden, and then the other one slides open. 单击其他链接之一时,打开的div将滑回隐藏状态,然后另一个幻灯片将打开。

What I am looking for, is to hide the divs again when the mouse is clicked on the link again, and also when the mouse is clicked outside the div (or anywhere on the page, really). 我要寻找的是,再次单击鼠标在链接上时以及在div之外(实际上是页面上的任何位置)单击鼠标时,再次隐藏div。

I have tried using "e.stopPropagation" but it doesn't seem to be working for me. 我尝试使用“ e.stopPropagation”,但它似乎不适用于我。

Any help is greatly appreciated - thank you. 非常感谢您的帮助-谢谢。

I have a jsFiddle that I created for practice: http://jsfiddle.net/AuU6D/3/ 我有一个为练习而创建的jsFiddle: http : //jsfiddle.net/AuU6D/3/

This is my jQuery code: 这是我的jQuery代码:

jQuery(function ($) {
    $('a.panel').click(function () {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active'),
            animIn = function () {
                $target.addClass('active').show().css({
                    left: -($target.width())
                }).animate({
                    left: 0
                }, 500);

            };

        if (!$target.hasClass('active') && $other.length > 0) {
            $other.each(function (index, self) {
                var $this = $(this);
                $this.removeClass('active').animate({
                    left: -$this.width()
                }, 500, animIn);                
            });
        } else if (!$target.hasClass('active')) {
            animIn();

    }

    });

});

Try 尝试

jQuery(function ($) {

    $('a.panel').click(function () {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active'),
            animIn = function () {
                $target.addClass('active').show().css({
                    left: -($target.width())
                }).finish().animate({
                    left: 0
                }, 500);

            };

        if (!$target.hasClass('active') && $other.length > 0) {
            $other.each(function (index, self) {
                var $this = $(this);
                $this.removeClass('active').animate({
                    left: -$this.width()
                }, 500, animIn);                
            });
        } else if (!$target.hasClass('active')) {
            animIn();
        } else if ($target.hasClass('active')) {
                $target.removeClass('active').finish().animate({
                    left: -$target.width()
                }, 500);                
        }

    });

    $(document).click(function(e){
        var $target = $(e.target), $active = $('div.panel.active');

        if($active.length && $target.closest('a.panel').length == 0 && $target.closest($active).length == 0){
                $active.removeClass('active').finish().animate({
                    left: -$active.width()
                }, 500);                
        }
    })

});

Demo: Fiddle 演示: 小提琴

DEMO 演示

 $(document).click(function (e) {
        if (!$(e.target).hasClass('panel')) {
            $('div.panel').removeClass('active').removeAttr('style').css('background', ' #e4e4e4');
        }
    });

or 要么

DEMO 演示

$(document).click(function (e) {
    console.log($(e.target).hasClass('panel'));
    if (!$(e.target).hasClass('panel')) {
        $('div.panel.active').removeClass('active').finish().animate({
            left: -$('#right').width()
        }, 500);
    }
});

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

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