简体   繁体   English

在两个类之间切换jQuery =可行,但额外点击

[英]toggling between two classes jQuery = works but extra click

<a href="#" class="sort">Sort</a> 


   $(".sort").click(function (event) {
        $(this).toggle(function() {
                $(this).toggleClass("sortUp","sortDown");
            }, function() {
                $(this).toggleClass("sortDown","sortUp");
           });
    });

it works but I need to click once before it works. 它有效,但是我需要先单击一次,然后它才能起作用。 so - click (nothing happens), click (sortUP), click (sortDown) 所以- 单击(什么都没有),单击(sortUP),单击(sortDown)

I would like to remove first click. 我想删除首次点击。

Thank you community for the help ! 谢谢社区的帮助!

Firstly, you're using toggleClass incorrectly. 首先,您使用的toggleClass错误。 You appear to want to toggle sortDown and sortUp on each click. 您似乎想在每次单击时切换sortDown和sortUp。 That's done with toggleClass("sortDown sortUp") . 这是通过toggleClass("sortDown sortUp")

Secondly, you need your class .sort to either have sortUp or sortDown set in its class property when you load the page. 其次,您需要在加载页面时在类.sortclass属性中设置sortUpsortDown eg <a href="#" class="sort sortDown"> . 例如<a href="#" class="sort sortDown"> This makes sure you can reason about your code (ie it's always true that exactly one of sortUp, sortDown are set on your div ). 这可以确保您可以对代码进行推理(即, 始终div上设置了sortUp, sortDown中的一个始终是正确的)。

Thirdly, $(this).click(function() { /* code */ }) means "when somebody clicks, do /*code*/ ". 第三, $(this).click(function() { /* code */ })意思是“当有人点击时,执行/*code*/ ”。 You've wrapped your 你已经包好了

$(this).click(function() { $(this).toggleClass("sortUp sortDown"); })

which sets up the click behaviour, in a $(".sort").click(function () { which means you are requiring an initial click on "sort" just to start the behaviour. 这会在$(".sort").click(function () {中设置点击行为,这意味着您需要先单击“ sort”才能开始行为。

So the correct version is: 因此正确的版本是:

<a href="#" class="sort sortDown">Sort</a> 


   $(document).ready(function () {
        $(".sort").click(function() {
                $(this).toggleClass("sortUp sortDown");
        });
    });

It looks like you are adding the click events on the first click, also if you want to switch between sortUp and sortDown you can simply specify them both. 看起来您是在第一次单击上添加了单击事件,如果要在sortUpsortDown之间sortUp ,也可以简单地将它们都指定。 As long as the element starts with one or the other (not both and not neither), it will swap them each time. 只要元素以一个或另一个(不是同时且不是两者都)开头,它将每次交换它们。

$(".sort").click(function() {
    $(this).toggleClass('sortUp sortDown');
});

You can see this running on JSFiddle . 您可以看到它在JSFiddle上运行。

if you dont' want to begin with a sortUp or sortDown class, do this: 如果您不想以sortUp或sortDown类开始,请执行以下操作:

<a href="#" class="sort">Sort</a> 

$(".sort").click(function (event) {            
    if($(this).hasClass("sortUp") || $(this).hasClass("sortDown")){
        $(this).toggleClass("sortUp sortDown");
    }else{
        $(this).addClass("sortUp");
    }
});

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

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