简体   繁体   English

当另一个切换时如何隐藏一个div?

[英]How to hide a div when another toggles?

I have an image inside a div that on click toggles another div and I want to hide the div that contains the image when the new div appears.我在 div 中有一个图像,单击时会切换另一个 div,我想在新 div 出现时隐藏包含该图像的 div。 When I use only the abreInfo() it works, but I also want to hide the container div.当我只使用 abreInfo() 时,它可以工作,但我也想隐藏容器 div。 How can I do it?我该怎么做?

function abreInfo(event, id) {
    event.preventDefault();
    $("#" + id).toggle("slow");
}

function fechardiv(div) {
    document.getElementById(div).style.display = "none";
}

http://codepen.io/Ryuh/pen/BzaNLL http://codepen.io/Ryuh/pen/BzaNLL

You can do it just with jQuery more easier like this:您可以使用 jQuery 更轻松地做到这一点,如下所示:

$('#container').on('click', function() {
  $(this).hide();
  $('#id').show();
});

Full code here https://jsfiddle.net/yLhq7ga6/完整代码在这里https://jsfiddle.net/yLhq7ga6/

Inside abreInfo() add $(event.currentTarget).closest('div').hide() .在 abreInfo() 中添加$(event.currentTarget).closest('div').hide()

.closest() gets the first ancestor that matches the selector. .closest()获取与选择器匹配的第一个祖先。 I'd make sure the container has a class, to make sure you hit the right element.我会确保容器有一个类,以确保您点击了正确的元素。

function abreInfo(event, id) {
    event.preventDefault();
    $("#"+id).toggle("slow");
    $(event.currentTarget).closest('div').hide("slow");
}

No need for abreInfo() function.不需要abreInfo()函数。 If you just want to display next div after hiding image.如果您只想在隐藏图像后显示下一个 div。 Just use your function like this.只需像这样使用您的功能。 Also remove abreInfo() function from onclick like this像这样onclick删除abreInfo()函数

function fechardiv(div){          
  $("#"+div).hide();
  $("#"+div).next().fadeIn();
}

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

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