简体   繁体   English

用jQuery隐藏父div

[英]Hiding a parent div with jQuery

i have a problem with jQuery. 我有一个jQuery的问题。 I have this: 我有这个:

在此输入图像描述

And i want that when you click on the black part of the page, the black part and the image dissapear. 我希望当你点击页面的黑色部分时,黑色部分和图像消失。 Here is my code: 这是我的代码:

HTML HTML

<div id="fade-wrapper">
    <div class="row-1-big"><img src="css/images/galeria/1.jpg"></div>
    <div class="row-2-big"><img src="css/images/galeria/2.jpg"></div>
    <div class="row-3-big"><img src="css/images/galeria/3.jpg"></div>
    <div class="row-4-big"><img src="css/images/galeria/4.jpg"></div>
    <div class="row-5-big"><img src="css/images/galeria/5.jpg"></div>
    <div class="row-6-big"><img src="css/images/galeria/6.jpg"></div>
    <div class="row-7-big"><img src="css/images/galeria/7.jpg"></div>
    <div class="row-8-big"><img src="css/images/galeria/8.jpg"></div>
    <div class="row-9-big"><img src="css/images/galeria/9.jpg"></div>
</div>

JS JS

var r1 = $('.row-1'); //this is for when you click the thumbnail

r1.click(function(){
   $('#fade-wrapper').show(300);
   $('.row-1-big').delay(400).show();
});

the problem is that if I add this code: 问题是,如果我添加此代码:

$('#fade-wrapper').click(function(){
   $('this').hide();
});

when i click the image (row-1-big) it hides all and I wan't that the content hide just when I click the black part. 当我点击图像(第1行 - 大)时,它隐藏了所有内容,而且当我点击黑色部分时,我不想隐藏内容。 If you don't understand just tell me. 如果你不明白只是告诉我。

Try to use e.stopPropagation() here to prevent click event fire on the parent div when you click on the child images: 尝试在此处使用e.stopPropagation()来防止在单击子图像时单击父div上的事件:

$('#fade-wrapper').click(function(){
   $(this).hide();
});

$('#fade-wrapper img').click(function(e){
   e.stopPropagation();
});

Also note that you don't need to wrap this inside quotes since this is an object not a string. 另请注意,您不需要this包装在引号内,因为this是一个对象而不是字符串。

To get the parent you would do - 为了得到父母,你会做 -

$('#fade-wrapper').click(function(){
    $(this).parent().hide(); 
});

If you are looking to click outside the Div wrapper which is the black area then close the whole thing, here is how: 如果你想在黑色区域的Div包装器外面点击然后关闭整个东西,这里是如何:

//Click on html 
$('html').click(function(){
  //hide
  $('#fade-wrapper').hide();
});

// prevent the fadeIn area to hide when clicked
$('#fade-wrapper').click(function(e){
   e.stopPropagation();
})

It should work if you check the div which is actually clicked with something like this : 它应该工作,如果你检查实际点击这样的div:

$('#fade-wrapper').click(function(e){
    if (e.target.id === 'fade-wrapper')
        $(this).hide();
});

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

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