简体   繁体   English

修改脚本以关闭Div

[英]Modify Script to Close Div

I've got a nice little show only one div at a time script working that I found at: Random Snippets (second demo down) that looks like this: 我有一个很好的小展示,它一次只显示一个div,我在以下位置找到它: 随机代码片段 (第二个演示向下),如下所示:

function showonlyone(thechosenone) {
     $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show(200);
          }
          else {
               $(this).hide(600);
          }
     });
}

The only thing it doesn't do is close the div after you open it. 它唯一不执行的操作是在打开div之后将其关闭。 Currently I have both divs set to display: none and would like to give the user the option to close them both when they are done looking. 目前,我将两个div都设置为显示:none,并希望为用户提供在完成查找后将它们都关闭的选项。

Any idea's how to modify this? 任何想法如何修改此?

You can have a click event that closes them when clicked: 您可以单击事件将其单击时将其关闭:

$('.newboxes').click( function() {
    $(this).hide(600);
});

You have to provide a button or something to the user to close the divs. 您必须向用户提供按钮或其他东西以关闭div。 Let's assume there is a button like this inside each div: 假设每个div中都有一个这样的按钮:

HTML 的HTML

<div class="div-class">
    <p>Box content</p>
    <button class="close">Close</button>
</div>

jQuery jQuery的

$('.close').on('click', function() {
   $(this).closest('.div-class').hide();
});

add a condition about the visibility of the current item in the if statement: if语句中添加有关当前项目可见性的条件:

function showonlyone(thechosenone) {
     $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone && $(this).css("display") == "none") {
               $(this).show(200);
          }
          else {
               $(this).hide(600);
          }
     });
}

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

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