简体   繁体   English

第二次单击以关闭切换div

[英]a second click to close a toggle div

I'm using the following jQuery for hiding and showing content (toggle), as a tree navigation menu in my website. 我正在使用以下jQuery来隐藏和显示内容(切换),作为我网站中的树形导航菜单。 I found this code extremely useful because by clicking, it displays only one div at a time. 我发现这段代码非常有用,因为通过单击它一次只显示一个div。

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function show_region(chosen_region) {
     $('.city_div').each(function(index) {
          if ($(this).attr("id") == chosen_region) {
               $(this).slideDown(200);
          }
          else {
               $(this).slideUp(600);
          }  
     });
}
</script>

<style type="text/css">
   .city_div {display: none;}
</style>


<a href="javascript:show_region('box01');">North</a><br>
<div class="city_div" id="box01">Div #01</div>

<a href="javascript:show_region('box02');">Centre</a><br>
<div class="city_div" id="box02">Div #02</div>

<a href="javascript:show_region('box03');">South</a><br>
<div class="city_div" id="box03">Div #03</div>

The problem is that I can close a div only by opening another div. 问题是我只能通过打开另一个div来关闭div。

How to close the div by a second click on it? 如何通过双击关闭div?

You can use slideToggle , change from slideDown to slideToggle : 您可以使用slideToggle ,从slideDown更改为slideToggle

function show_region(chosen_region) {
     $('.city_div').each(function(index) {
          if ($(this).attr("id") == chosen_region) {
               $(this).slideToggle(200); // instead of slideDown
          }
          else {
               $(this).slideUp(600);
          }  
     });
}

First of all, don't use inline event handlers. 首先,不要使用内联事件处理程序。

<script type="text/javascript">
$(document).ready(function() {
    var boxes = $('.city_div');

    $('.city-toggle').click(function(e) {
        var box = $(this).next();
        var isHidden = box.is(':hidden');

        boxes.slideUp(600);

        if(isHidden) {
            box.slideDown(200);
        }

        e.preventDefault();
    });
});
</script>

<style type="text/css">
   .city_div {display: none;}
</style>


<a href="something-meaningful-like-north.html" class="city-toggle">North</a>
<div class="city_div" id="box01">Div #01</div>

<a href="ditto.html" class="city-toggle">Centre</a>
<div class="city_div" id="box02">Div #02</div>

<a href="same.html" class="city-toggle">South</a>
<div class="city_div" id="box03">Div #03</div>

Also, if your links don't go anywhere meaningful, use # as the href and make sure the boxes are visible by default. 另外,如果您的链接没有任何意义,请使用#作为href ,并确保默认情况下该框可见。 (Hide them using boxes.hide(); right at the start.) (在开始时使用boxes.hide();隐藏它们。)

Also also, <br> isn't what you should use there. 另外, <br>也不应该在这里使用。 <div> s are already block-level elements. <div>已经是块级元素。 If you want more padding, give them a top margin. 如果您想要更多的填充,给他们一个上边距。

You should keep a variable with your old chosen region name 您应该使用以前选择的地区名称保留一个变量

<script type="text/javascript">
    var old_chosen_region="";
    function show_region(chosen_region) {
        $('.city_div').each(function(index) {
            if (($(this).attr("id") == chosen_region)&&(chosen_region!=old_chosen_region)) {
                $(this).slideDown(200);
                old_chosen_region=chosen_region;
            } else {
                $(this).slideUp(600);
            }
        }
        old_chosen_region='';
    });
</script>

This allows you to 'remember' which region you chose last and, if the chosen one equals the last you close it. 这使您可以“记住”您最后选择的区域,如果所选区域等于最后一个区域,则可以将其关闭。

You should set old_chosen_region='' in the end to let the tab reopen. 您应该在最后设置old_chosen_region=''以重新打开选项卡。

try this: 尝试这个:

$(document).ready(function(){
    $('.city_div').click(function(){
        $(this).hide(); //or whatever code you want to hide it
    });
});

that way when you click on a div, it will hide it. 这样,当您单击div时,它将隐藏它。

Since you are using jQuery you could try using the jQuery.toggle function. 由于您使用的是jQuery,因此可以尝试使用jQuery.toggle函数。

function show_region(chosen_region) {
     $('#' + chosen_region).toggle('slide');
     return false;
}

First, why are you using such an old version of jQuery? 首先,为什么要使用旧版本的jQuery?

Second, your JavaScript can be simplified. 第二,可以简化您的JavaScript。 jQuery works on sets; jQuery适用于集合; you don't need the .each , or loops. 您不需要.each或循环。

Here is a working example: http://jsfiddle.net/gibble/25P9z/ 这是一个工作示例: http : //jsfiddle.net/gibble/25P9z/

Simplified HTML: 简化的HTML:

<a href="#" data-contentDiv="box01">North</a><br>
<div class="city_div" id="box01">Div #01</div>

<a href="#" data-contentDiv="box02">Centre</a><br>
<div class="city_div" id="box02">Div #02</div>

<a href="#" data-contentDiv="box03">South</a><br>
<div class="city_div" id="box03">Div #03</div>​

New JavaScript: 新的JavaScript:

function show_region(e) {
    var $target = $(e.target);
    var chosen_region = $target.attr('data-contentDiv');

    var openDiv = $('#' + chosen_region);

    openDiv.slideDown(200);
    $('.city_div').not(openDiv).slideUp(600);
}

Last, attach events, don't inline them in your HTML. 最后,附加事件,不要在HTML中内联它们。

$('a[data-contentDiv]').click(show_region);​

Keep track of the div you have clicked last: 跟踪上次单击的div:

var globalLastClickedButtonId;
$("div.city_div").click(function() {
    if (globalLastClickedButtonId == $(this).attr("id")) {
        $(this).hide();
    } else {
        var box = $(this).next().next();
        var clickedId = $(this).attr("id");
        $("div.city_div").each(function() {
           if ($(this).attr("id") == clickedId)
              box.slideDown(200);
           else
              box.slideUp(600);
        }
    }
    globalLastClickedButtonId = $(this).attr("id");
});

Because you are using an animation it is best to keep track of the div that is currently shown. 因为您使用的是动画,所以最好跟踪当前显示的div。 If you don't, then you will start to see multiple divs showing if you click links in quick succession. 如果您不这样做,那么您将开始看到多个div,如果您单击快速连续的链接,它们会显示出来。

You can use this: 您可以使用此:

$(function() {
    $("a").click(function(e) {
        e.preventDefault();
        var selectedDiv = $("#" + $(this).attr("href"));
        var hide = selectedDiv.data("shown");

        $(".city_div").slideUp(600);
        $(".city_div").data("shown", false);

        if (hide) {
            selectedDiv.data("shown", false);
        }
        else {
            selectedDiv.data("shown", true);
            selectedDiv.slideDown(200);
        }
    });
});​

Here is a working example 这是一个有效的例子

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

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