jQuery Switchclass不断切换回去 [英] jQuery Switchclass keeps switching back
问题描述
What I am trying to do is to make a button on click, go to the top left slot in the crafting table on this page. Then if the element is clicked again it will go back. The specific jQuery I am using is:
$("#sword").click(function () {
if ( $("#sword").hasClass("s1") ) {
$('#sword.s1').switchClass( "s1", "sd1", 1000, "linear" );
} else if ( $("#sword").hasClass("sd1") ) {
$('#sword').switchClass( "sd1", "s1", 1000, "linear" );
$('#bukkit').switchClass( "s1", "sd2", 1000, "linear" );
$('#grass').switchClass( "s1", "sd3", 1000, "linear" );
}
});
sd1
在下面,s1
在上面.如您所见,当前它会反弹,然后立即反弹回去.我曾尝试使单击.s1
时的弹跳功能恢复为单独的功能,但是单击该键时什么也不会发生.
sd1
is down below, s1
is the slot above. As you can see currently it will bounce up and then immediately bounce back down. I have tried making bounce back down a separate function for when .s1
is clicked, but then nothing would happen when I clicked it.
推荐答案
只需在链接到的页面上检查您的代码.您已将两个单击处理程序绑定到#sword
.您只显示了第一个.您的代码如下
Just checked your code on the page you linked to. You have bound two click handlers to #sword
. You have only shown the first one. Your code is as follows
$("#sword").click(function () { // handler 1
if ( $("#sword").hasClass("s1") ) {
$('#sword.s1').switchClass("s1", "sd1", 1000, "linear");
} else if ($("#sword").hasClass("sd1")) {
$('#sword').switchClass( "sd1", "s1", 1000, "linear" );
$('#bukkit').switchClass( "s1", "sd2", 1000, "linear" );
$('#grass').switchClass( "s1", "sd3", 1000, "linear" );
}
});
// a few lines later
$("#sword").click(function() { // handler 2
$('#sword').switchClass( "s1", "sd1", 1000, "linear" );
$('#bukkit').switchClass( "s1", "sd2", 1000, "linear" );
$('#grass').switchClass( "s1", "sd3", 1000, "linear" );
});
单击剑将首先执行处理程序1,然后执行处理程序2.从代码中可以清楚地看出,处理程序1将其向上移动,处理程序2将其向下移动.
Clicking on the sword executes handler 1 first and then handler 2. As is clear from the code, handler 1 moves it up and handler 2 brings it down.
如果您解释了为什么编写第二个处理程序,那么我也许可以提供更多帮助.
If you explain why you wrote the second handler, I might be able to help more.
这篇关于jQuery Switchclass不断切换回去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!