简体   繁体   English

使用jQuery在div外部单击时隐藏内容

[英]Hide content when clicking outside div with jquery

I'm using jquery to togle content with a button, I would like to hide the content when I click outside my "contentcone" div. 我正在使用jquery来通过按钮切换内容,当我在“ contentcone” div之外单击时,我想隐藏内容。 The HTML is the following HTML如下

<div class="togglecone">
        </div>
        <div class="contentcone">
            <div class="contentleft">
                <div class="title">
                Cone
                </div>
                <div class="maincopy">
                Hello my friends this is a really nice cone that can be placed anywhere
                </div>
                <a href="https://www.mcnicholas.co.uk/" class="button">
                View on website
                </a>
            </div>
            <div class="contentright"> <img src="images/cone.png" alt=""/>
    </div>
</div>

This is the script 这是脚本

 $(document).ready(function(){
      var $content = $(".contentcone").hide();
    $(".togglecone").on("click", function(e){
    $(this).toggleClass("expandedcone");
    $content.slideToggle();
    });
    });

http://jsfiddle.net/thomastalavera/SCKhf/914/ http://jsfiddle.net/thomastalavera/SCKhf/914/

This should do it: 应该这样做:

$(document).ready(function(){
    var $content = $(".contentcone").hide();
    $(document).on("click", function(e) { 
        if( $(e.target).is(".togglecone") ) {       
            $(this).toggleClass("expandedcone");                                        
            $content.slideToggle();

        } else {
            $content.slideUp();
        }
    });
});

DEMO 演示

A simple and pretty blunt way to do this is: 一个简单而简单的方法是:

 $(document).ready(function(){
   var $content = $(".contentcone").hide();
   $(".togglecone").on("click", function(e){  
      e.stopImmediatePropagation();
      $(this).toggleClass("expandedcone");
      $content.slideToggle();
   });
   $("body").on("click", function(e){
       if ($(".contentcone").is(':visible')) {
          $(".togglecone").click();
       }
   });
   $(".contentcone").on("click", function(e){
      e.stopImmediatePropagation();
      return false;
   })
 });

But note it has a lot of disadvantages, is just a blunt solution to your problem, it must be tweaked to be ok as a permanent choice. 但是请注意,它有很多缺点,只是对您问题的一种直截了当的解决方案,必须对其进行调整以使其成为永久选择。

Edit (to answer the question in comment): 编辑(以回答评论中的问题):

Sure, I know more than 1, each depending on your layout. 当然,我知道不止1个,每个都取决于您的布局。 You can: 您可以:

a) Instead of the "body" part, make a selector for whatever elements you want to toggle event one. a)代替“主体”部分,为想要切换事件一的任何元素选择一个选择器。 This works ok on layouts with a small number of big (as size on screen) elements. 在具有少量大元素(与屏幕上的大小一样)的布局上,这可以正常工作。

b) Add one more condition to the "body" part, where you get mouse position and use it to see if the mouse is in the place you want. b)在“身体”部分添加另一个条件,在该位置您将获得鼠标位置,并使用它来查看鼠标是否在所需位置。 You can do this with e.pageX/e.pageY, or you can find relevant relative position to an element here jQuery get mouse position within an element . 您可以使用e.pageX / e.pageY进行此操作,也可以在此处找到与元素相关的相对位置jQuery,以获取元素内的鼠标位置

You need to set a click event on document to close the box. 您需要在文档上设置一个单击事件以关闭该框。 I tried to keep your original click function intact. 我试图保持原始点击功能不变。

$(document).ready(function(){
  var $content = $(".contentcone").hide();
  $(".togglecone").on("click", function(e){
    $(this).addClass("expandedcone");
    $content.slideDown();
  });

  $(this).on('click', function(e) {
    if ($(e.target).is('.togglecone')) { // don't slide up if you click the cone
      return;
    }
    if ($(".togglecone").hasClass('expandedcone')) {
      $content.slideUp();
      $(this).removeClass("expandedcone");
    }
  });
});

http://jsfiddle.net/SCKhf/925/ http://jsfiddle.net/SCKhf/925/

This should do it with lesser code: 这应该用更少的代码来完成:

$(document).mousedown(function (e) {

    var container = $(".togglecone");

    if (!container.is(e.target) && container.has(e.target).length === 0)
    {
        container.fadeOut('slow');
    }

});

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

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