简体   繁体   English

使用jQuery切换div的显示

[英]Toggling display of a div with jQuery

I have two links, one with id #calendar_arrival_open and another with #calendar_departure_open. 我有两个链接,一个是id #calendar_arrival_open,另一个是#calendar_departure_open。 Both links control whether or not a div containing a calendar is displayed, but in both cases the calendar displayed is the same calendar, to avoid loading two identical calendars. 两个链接都控制是否显示包含日历的div,但在这两种情况下,显示的日历都是相同的日历,以避免加载两个相同的日历。 I'm trying to use the following code to toggle the opening and closing of the calendar when the links are clicked. 我正在尝试使用以下代码在单击链接时切换日历的打开和关闭。

var state = "closed";
if(state == "closed"){
    $("#calendar_arrival_open").click(function () {
        $("#calendar_box").show();
        $("#select_arrival_date").show();
        $("#select_departure_date").hide();
        state = "arrival_open";
    });
    $("#calendar_departure_open").click(function () {
        $("#calendar_box").show();
        $("#select_departure_date").show();
        $("#select_arrival_date").hide();
        state = "departure_open";
    });
}

if(state == "arrival_open"){
    $("#calendar_arrival_open").click(function () {
        $("#calendar_box").hide();
        state = "closed";
    });
    $("#calendar_departure_open").click(function () {
        $("#select_departure_date").show();
        $("#select_arrival_date").hide();
        state = "departure_open";
    });
}

if(state == "departure_open"){
    $("#calendar_arrival_open").click(function () {
        $("#calendar_box").hide();
        $("#select_departure_date").hide();
        $("#select_arrival_date").show();
        state = "arrival_open";
    });
    $("#calendar_departure_open").click(function () {
        $("#calendar_box").hide();
        state = "closed";
    });
}

This code works for opening the calendar, but not for closing it. 此代码适用于打开日历,但不适用于关闭日历。 I can't see why. 我不明白为什么。 As you can see, if the "arrival calendar" is open and the departure calendar link is clicked, the "departure calendar" appears, and vice-versa. 如您所见,如果“到达日历”已打开并且单击了离开日历链接,则会出现“出发日历”,反之亦然。 However, if the arrival calendar is open and the arrival calendar link is clicked, then the calendar closes. 但是,如果到达日历已打开并且点击了到达日历链接,则日历将关闭。

Can anyone see the problem? 有谁能看到这个问题? Is this "state" method the best for handling what I need? 这种“状态”方法最适合处理我需要的东西吗?

Try this: 尝试这个:

var state = "closed";

$("#calendar_arrival_open").click(function () {
    if(state == "closed"){

        $("#calendar_box").show();
        $("#select_arrival_date").show();
        $("#select_departure_date").hide();
        state = "arrival_open";

    } else if(state == "arrival_open"){

        $("#calendar_box").hide();
        state = "closed";

    } else {

        $("#calendar_box").hide();
        $("#select_departure_date").hide();
        $("#select_arrival_date").show();
        state = "arrival_open";

    }
});

$("#calendar_departure_open").click(function () {
    if(state == "closed"){

        $("#calendar_box").show();
        $("#select_departure_date").show();
        $("#select_arrival_date").hide();
        state = "departure_open";

    } else if(state == "arrival_open"){

        $("#select_departure_date").show();
        $("#select_arrival_date").hide();
        state = "departure_open";

    } else {

        $("#calendar_box").hide();
        state = "closed";

    }
});

The code that runs when a click event occurs must be defined inside a click event handler. 发生单击事件时运行的代码必须在click事件处理程序中定义。 On your code, you want the state check to be performed on every click, but you added the if statement outside the event handlers. 在您的代码中,您希望每次单击都执行状态检查,但是在事件处理程序之外添加了if语句。 Then you are stuck with those handlers from your first if block. 然后你会被第一个if块中的那些处理程序困住。

Your code says: "when state is x , define a click event handler to do a ; when state is y , define a click event handler to do b ;". 您的代码说:“当时statex ,定义一个click事件处理程序做a ;当statey ,定义一个click事件处理程序做b ;”。

What you want is: "when an element is clicked, do a if state is x , or b if state is y ". 你想要的是:“被点击元素时,做a如果statex ,或b如果statey ”。

See the difference? 看到不同?

You could use a switch block to achieve this. 您可以使用开关块来实现此目的。 I'm not sure if that's quicker programatically, but it looks easier to maintain to me. 我不确定这是否会以编程方式更快,但对我来说看起来更容易。

This obviously needs to be placed inside a $(document).ready() handler. 这显然需要放在$(document).ready()处理程序中。

var state = "closed";

$("#calendar_arrival_open").click(function () {

switch(state){

case "closed":
    $("#calendar_box").show();
    $("#select_arrival_date").show();
    $("#select_departure_date").hide();
    state = "arrival_open"; 
break;
case "arrival_open":
    $("#calendar_box").hide();
    state = "closed";   
break;  
case "departure_open":
    $("#calendar_box").hide();
    $("#select_departure_date").hide();
    $("#select_arrival_date").show();
    state = "arrival_open";
break;  
}
})

$("#calendar_departure_open").click(function () {

switch(state){

case "closed":
    $("#calendar_box").show();
    $("#select_departure_date").show();
    $("#select_arrival_date").hide();
    state = "departure_open";
break;
case "arrival_open":
    $("#select_departure_date").show();
    $("#select_arrival_date").hide();
    state = "departure_open";
break;  
case "departure_open":
    $("#calendar_box").hide();
    state = "closed";
break;  
}
})

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

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