首页 > javascript > jQuery slideToggle不断重复

jQuery slideToggle不断重复 (jQuery slideToggle keeps repeating)

问题

我有一个简单的jQuery代码,如下所示:

$( "#resp-navigation" ).one("click", function() {
    $( "#navigation" ).slideToggle(500);
});

我的HTML看起来像这样:

<div id="resp-navigation"> ... </div>
<nav id="navigation"> ... </nav>

我的问题是,当我点击#resp-navigation div时,#navigation继续切换。有谁知道为什么?

谢谢。

解决方法

如果您打算每次有人点击“#resp-navigation”div时都要进行“#navigation”div切换,那么:

这个: $( "#resp-navigation" ).one

应该: $( "#resp-navigation" ).on

然后它在这个小提琴中运行良好:

http://jsfiddle.net/us0dm62c/

但是,如果你的意图是只有'#navigation'div切换一次(因此它消失并且无法再次显示),那么你的代码完全没有修改,正如你在这个小提琴中看到的那样:

http://jsfiddle.net/gktnhhm5/

问题

I have a simple jQuery code that looks like this:

$( "#resp-navigation" ).one("click", function() {
    $( "#navigation" ).slideToggle(500);
});

My HTML looks like this:

<div id="resp-navigation"> ... </div>
<nav id="navigation"> ... </nav>

My problem is that when I click on the #resp-navigation div, the #navigation keeps toggling. Does anyone meybe know why?

Thanks.

解决方法

If your intention is to have the '#navigation' div toggle each time someone clicks on the '#resp-navigation' div, then:

This: $( "#resp-navigation" ).one

Should be: $( "#resp-navigation" ).on

And then it works fine in this fiddle:

http://jsfiddle.net/us0dm62c/

If, however, your intention is to only ever have the '#navigation' div toggle once (so it disappears and cannot be shown again), then your code does exactly that with no modifications, as you can see in this fiddle:

http://jsfiddle.net/gktnhhm5/

相似信息