简体   繁体   English

Div按钮切换div-Javascript / CSS

[英]Div button to toggle divs - Javascript/CSS

I'm trying to add a "back button" which will toggle the current div to the previous div. 我正在尝试添加“后退按钮”,它将当前div切换到上一个div。 Basically, I have a div of an Oklahoma map which fills the window. 基本上,我有一个充满俄克拉荷马州地图的div窗口。 When the user clicks a certain area of the map, the div is toggled with a div that is the image of the "zoomed-in" area which they selected. 当用户单击地图的某个区域时,将使用一个div切换div,该div是他们选择的“放大”区域的图像。 I want my back button to appear on top of this zoomed-in div, in the bottom right corner, and if the user clicks the button, the div will be toggled with the original Oklahoma map div. 我希望我的后退按钮显示在此放大div的右下角,如果用户单击该按钮,则该div将与原始俄克拉荷马州地图div切换。

Here's the code I've tried so far: 这是到目前为止我尝试过的代码:

CSS: CSS:

#backButton
{
    position:absolute;
    bottom:50px;
    right:50px;
    background:url('images/backspace.png');
    background-repeat:no-repeat;
    z-index:2;
    height:50px;
    width:50px;
}
.button
{
    position:absolute;
    bottom:100px;
    right:100px;
    background:url('images/backspace.png');
    background-repeat:no-repeat;
    z-index:2;
    cursor:pointer;
}

HTML: HTML:

<div id="backButton" style="display:none; background:url('images/backspace.png');" onClick="#container.toggle();">
    <div class="button"></div>
</div>

The button appears correctly, but it isn't functioning. 该按钮正确显示,但是不起作用。 I'm sure it has to do with onClick="#container.toggle();" 我确定这与onClick="#container.toggle();" but I don't know how to do it correctly. 但我不知道如何正确地做。

Just for background information, the container is the original Oklahoma map and it is toggled off in my JS code ( $("#container").toggle(); ) when I switch to a zoomed-in div. 仅作为背景信息,该容器是原始的俄克拉荷马州地图,当我切换到放大的div时,将在我的JS代码中关闭该$("#container").toggle();$("#container").toggle(); )。

EDIT: Ok, so I do have Jquery and I tried making a click event like my other toggle events: 编辑:好的,所以我确实有Jquery,并且尝试像其他切换事件一样使click事件发生:

 $("#backButton").click(function(e)
{
    $("#container").toggle();
            $("#backButton").toggle();
});

--> I removed the onClick part in the div. ->我删除了div中的onClick部分。 The button will toggle off when I click it, but the container div (Oklahoma map) is not toggling back on. 单击该按钮后,该按钮将关闭,但容器div(俄克拉荷马州地图)未重新切换。

You want your onClick method to call a valid function such as abcMethod() . 您希望onClick方法调用有效的函数,例如abcMethod() (Wherever your toggle code lives). (无论您的切换代码是否有效)。

As you are using jQuery (evident in your code) 当您使用jQuery(在您的代码中显而易见)

Put this in your javascript: 把它放在你的JavaScript中:

$("#button").click(function(){
  $("#backButton").toggle();// hide the div (assume you have it turned on somewhere
  $("#container").toggle();) // show the other div
});

remove the onClick="#container.toggle();" 删除onClick="#container.toggle();" from the markup 从标记

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

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