简体   繁体   English

jQuery在不应该切换的时候

[英]jQuery Toggling when it shouldn't

So I got myself this far and I've solved the problem I was having but I'd still like to know why this is. 所以我走了这么远,已经解决了我遇到的问题,但我仍然想知道为什么会这样。

First, here is my original code: 首先,这是我的原始代码:

$(function(){
    var navListLength = $('nav ul li').length;

    buttonPress(1);
    function buttonPress(i){

        if(i < navListLength){

            $('nav ul li a:nth-child(' + i + ')').click(function(){
                $(this).toggleClass('button-pressed');

                console.log('Button Down');

                setTimeout(function(){
                    $('nav ul li a:nth-child(' + i + ')').toggleClass('button-pressed');

                    buttonPress(i + 1);
                    console.log('Button Up');

                }, 500);
            });
        }
    }
});

It's supposed to toggle the style of a single link in a navigation list when it's clicked and not affect any other links in the list. 它应该在单击时切换导航列表中单个链接的样式,并且不影响列表中的任何其他链接。

I wanted to do it this way so that I could add any amount of links to this list in the future without having to create a new a new class and add it to the jQuery each time. 我想这样做,以便将来可以将任何数量的链接添加到此列表,而不必创建新的新类并将其每次添加到jQuery。

The first toggle works as expected. 第一个切换按预期工作。 But the second toggle is being applied to all of the links and it creates an inverted effect. 但是第二个切换将应用于所有链接,并且会产生相反的效果。 So all of the links are opposite of what they are supposed to be except the link being clicked. 因此,除了被单击的链接以外,所有链接都与它们应有的相反。

I solved the problem by changing the first toggleClass to addClass and the second toggleClass to removeClass. 我通过将第一个toggleClass更改为addClass,将第二个toggleClass更改为removeClass来解决了该问题。 But I shouldn't have to do that. 但我不必这样做。

Could anyone tell me why this is? 谁能告诉我为什么呢?

Here's a picture of my buttons: 这是我的按钮的图片:

我的菜单按钮

You're doing way too much work just to toggle a class on click. 您要做太多的工作,只是为了在点击时切换课程。 You don't need to make an array of all the navigation items, the whole point of jQuery is that it handles selecting DOM elements for you. 您不需要将所有导航项都制成数组,jQuery的全部意义在于它可以为您选择DOM元素。

Try this: 尝试这个:

$(function() {

  $('nav ul li').click(function() {
    $(this).toggleClass('button-pressed');
  });

});

You don't have to handle making sure the others aren't affected. 您不必确定其他人没有受到影响。 Only the clicked element gets toggled. 仅单击元素被切换。

-- EDIT -- -编辑-

I see what you were going for with the setTimeout() now. 我现在看到了使用setTimeout() You can do it inside the click handler, like this: 您可以在点击处理程序中执行此操作,如下所示:

  $('nav ul li').click(function() {
    // store the selection in a variable
    var $this = $(this);
    $this.toggleClass('button-pressed');
    window.setTimeout(function() {
      $this.toggleClass('button-pressed');
    }, 500);
  });

FIDDLE 小提琴

Why now in pure CSS like: 为什么现在使用纯CSS之类的东西:

LIVE DEMO 现场演示

 <span class="button" tabindex="1"> 
   <span>Home</span>
 </span>

span.button{
  display:inline-block;
  border-radius:9px;
  background:#ddd;
  padding:6px;
  vertical-align:middle;
  outline:none;
}
span.button > span{
  background:#F06BAE;
  color:#fff;
  font-size:20px;
  font-weight:bold;
  display:inline-block;
  padding:10px 25px;
  border-radius: 6px;
  border-bottom:4px solid #CB4589;
  transition: border 0.3s;
}
span.button:focus > span{
  border-top: 4px solid #FCA9D2;
  border-bottom-width:0;
}

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

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