开发者

jQuery to show and hide divs

开发者 https://www.devze.com 2023-02-28 03:55 出处:网络
I\'m trying to use jQuery to show and hide two overlay DIVs when I click on links in the footer (see markup below). The jQuery behaves as expected if I\'ve clicked on one of the links: it hides the ot

I'm trying to use jQuery to show and hide two overlay DIVs when I click on links in the footer (see markup below). The jQuery behaves as expected if I've clicked on one of the links: it hides the other overlay and shows the DIV that matches the link I clicked on. If I click on Terms and Conditions and then click on Terms and Conditions again, it hides the DIV and then shows the same DIV again. I want to hide the DIV if it's already visible. (I initially tried .toggle and the behavior was the same.)

Links in the footer:

<a href="#terms">Terms and Conditions</a>
<a href="#privacy">Privacy Policy</a>

DIVs in the HTML:

<div class="meta" id="terms">Terms and Conditions</div>
<div class="meta" id="privacy">Privacy Policy</div>

jQuery:

$('footer a').click(function(e){
    $('.meta').hide();
    var div_to_show = $(this).attr('href');
    if($(div_to_show).is(':visible')) {
        // hide corresponding div if it's visible
        $(div_to_show).hide('fast');
    } else {
        // show corresponding div if it's not visible
        $(div_to_show).show('fast');
    }
    e.preventDefa开发者_Python百科ult();
});

This worked:

if($(this.hash).is(':visible')) {
    $('.meta').hide('fast');
} else {
    $('.meta').hide('fast');
    $(this.hash).show('fast');
}
e.preventDefault();


You're hiding both divs before checking if the clicked link's associated div is :visible.


The right way to do it:

var $meta = $('.meta');
$('footer a').click(function(e){
    var $div = $($(this).attr('href'));
    $meta.not($div).hide();
    $div.toggle('fast');
    return false;
});
  • Use .toggle()
  • Cache jQuery selectors when appropriate

Demo


Why don't you set a visible flag?

var isVisible = false;
$('footer a').click(function(e){
    $('.meta').hide();
    var div_to_show = $(this).attr('href');
    if(isVisible) {
        // hide corresponding div if it's visible
        $(div_to_show).hide('fast');
    } else {
        isVisible = true;
        $(div_to_show).show('fast');
    }
    e.preventDefault();
});


Here is the code to show hide divs...

JsFiddle

$(document).ready(function () {
    $("#message_div").hide();
    //attach click event to buttons
    $('.button-show').click(function () {

        /**
         * when show button is clicked we call the show plugin
         * which scales the box to default size
         * You can try other effects from here: http://jqueryui.com/effect/
         */
        $("#message_div").show();

    });

    $('.button-hide').click(function () {

        //same thing happens except in this case we hide the element
        $("#message_div").hide();

    });
});

and in the template/html file.

<div id="message_div" style="width:80%; margin:0 auto; color:black;">
    <h1> This is Div One </h1>
    <p>  Your Content</p>
 <br />
<input type="button" value="Discard" class="button-hide" />
 <br />
</div>
<div style="width:80%; margin:0 auto; color:black; margin-top:25px">
<input type="button" value="Send Message" class="button-show"  />   
</div>
<div style="width:80%; margin:0 auto; margin-top:25px">
    <h1>This is Div Two</h1>
<p>Your Content</p>
</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消