jQuery:在元素的任何其他位置单击时隐藏元素 [英] jQuery: Hide element on click anywhere else apart of the element

查看:117
本文介绍了jQuery:在元素的任何其他位置单击时隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何检测元素外的点击?

我正在努力实现 div 隐藏,如果用户点击除元素之外的任何地方。如果用户点击按钮,我有以下代码执行 toggle()。如果 Details 元素可见,我希望按钮点击保持加号,对屏幕的其他部分做出反应。

I'm trying to achieve that a div hides if user clicks anywhere except on the element. I've got following code doing toggle() if user clicks on a button. I want the button click to stay plus if the Details element is visible, reacts to other parts of the screen.

$('.nav-toggle').click(function() {
  //get collapse content selector
  var collapse_content_selector = $(this).attr('href');

  //make the collapse content to be shown or hide
  var toggle_switch = $(this);
  $(collapse_content_selector).toggle(function() {
    if ($(this).css('display') == 'none') {
      //change the button label to be 'Show'
      toggle_switch.html('Show Details');
    } else {
      //change the button label to be 'Hide'
      toggle_switch.html('Hide Details');
    }
  });
});




推荐答案

你可以度假活动授权的概念。

$(function() {
    $(document).on('click', function(e) {
        if (e.target.id === 'div1') {
            alert('Div Clicked !!');
        } else {
            $('#div1').hide();
        }

    })
});​

检查 FIDDLE

我不明白你与其他部分整合的意思..这是基本的想法..

I did not understand what you meant by integrating with the other part.. This is the basic idea..

这篇关于jQuery:在元素的任何其他位置单击时隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆