简体   繁体   English

jQuery:div出现并正常消失,但是第二次单击它不起作用

[英]JQuery: div appears and disappears normally but second time clicked it does not work

I am pretty new to JQuery. 我对JQuery很陌生。 I was looking for divs to hide and show in my homepage. 我一直在寻找隐藏并显示在首页中的div。 This worked with following code pretty good: 使用以下代码可以很好地工作:

jQuery(document).ready(function () {
jQuery(".tab-filteractivation2").hide(); //hide at the beginning

function hideDiv(e) {
    e.preventDefault();
    jQuery(this).text('open it')  // text after first click
    .click(showDiv)
    .siblings(".tab-filteractivation2").hide(400);
}
function showDiv(e) {
    e.preventDefault();
    jQuery(this).text('close it')  // text when it is open
    .click(hideDiv)
    .siblings(".tab-filteractivation2").show(400);
}
jQuery('.hover').click( showDiv );

This is my HTML-code: 这是我的HTML代码:

<div class="filter">
    <a class="hover" href="javascript:;">open</a>
    <div class="tab-filteractivation2">test</div>
</div>

My DIV is called tab-filteractivation2 and it appears and disappears as I want. 我的DIV称为tab-filteractivation2,它根据需要显示和消失。 But only for the first click: As soon as I click more than one time the show button, it appears somehow exponential. 但仅适用于第一次点击:只要我多次单击显示按钮,它就会以指数形式显示。

Here is a little step-by-step introduction: 1. I click on "open" and the tab appears with the delay (400) (with the text "test") 2. I click on "close it" and the tab disappears with the delay (400) (until here everything is fine) 3. I click again on "open it" and the tab appears BUT first it appears with the delay (400) and then immediately disappears with the delay (400) and then again appears with the delay (400). 以下是一些分步介绍:1.单击“打开”,选项卡出现延迟(400)(带有文本“ test”)。2.单击“关闭”,选项卡消失带有延迟(400)(直到这里一切都很好)3.我再次单击“打开它”,然后该选项卡出现,但首先出现带有延迟(400)的选项卡,然后立即消失并带有延迟(400),然后再次消失出现延迟(400)。 4. I click on "close it" and the tap disappears with the delay (400) and it appears with the delay and it disappears with the delay and it appears with the delay and it disappears with the delay (400). 4.我单击“关闭它”,该抽头随着延迟(400)消失,并随着延迟出现,随着延迟而消失,随着延迟而出现,随着延迟(400)而消失。

Therefore I wrote it is somehow exponential. 因此我写了它是指数的。 It will be from click by click more and more actions until it is finished. 这将是单击并单击越来越多的操作直到完成。 But I would be happy only having this delay once and all the other steps not. 但是我很高兴只延迟一次,其他所有步骤都没有。

Can someone help me by this function and say how I could prevent it? 有人可以通过此功能帮助我并说出如何防止它吗? That would be great! 那很好啊!

Many thanks in advance. 提前谢谢了。 And I hope it was somehow clear for you guys. 我希望这对你们来说是清楚的。

Try: 尝试:

jQuery(".tab-filteractivation2").hide(); //hide at the beginning

function showDiv(e) {
    e.preventDefault();
    if(jQuery(this).text() == 'close it'){
        jQuery(this).text('open it').siblings(".tab-filteractivation2").hide(400);
    }
    else{        
        jQuery(this).text('close it').siblings(".tab-filteractivation2").show(400);
    }

}
jQuery('.hover').click( showDiv );

DEMO here. 演示在这里。

You add an event listener every time you hide or show the tab. 每次隐藏或显示选项卡时,都会添加一个事件侦听器。 You need to initialize nodes' listeners separately. 您需要分别初始化节点的侦听器。

$('.hover').on('click', function() {
    if($('tab-filteractivation2').is(':visible')) {
        $('.tab-filteractivation2').slideUp(400);
    } else {
        $('.tab-filteractivation2').slideDown(400);
    }
});

The code above will do what you are trying to do in a much more simple manner. 上面的代码将以更简单的方式完成您要尝试的操作。 If you are struggling to understand how it works give me a shout :). 如果您正在努力了解它的工作原理,请大声疾呼:)。

you could use toggle() : 您可以使用toggle()

jQuery('.hover').click(function (e) {
    e.preventDefault();
    var $this = $(this);
    $this.siblings(".tab-filteractivation2").stop().toggle(400, function () {
       $this.text($(this).is(':visible') ? 'close it' : 'open it');
    });
});

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

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