我正在尝试做一个基本的 javascript/jquery 动画。基本上,在普通 View 下隐藏的 div 在单击按钮时应该会出现。
我遇到的问题在于条件(if else
语句)。
这是我正在使用的代码。
$(document).ready(function () {
animationClick('#animateThis', '#someElement', '#startHere');
});
function animationClick(element, secondElement, elementToBeClicked) {
element = $(element);
elementToBeClicked = $(elementToBeClicked);
secondElement = $(secondElement);
var state = 0;
var containerWidth = $('#container').width();
elementToBeClicked.on("click", function () {
if(state == 0) {
secondElement.animate({
top: '27%',
}, 500);
element.animate({
left: '0%',
}, 500);
elementToBeClicked.html("Hide");
state = 1;
} else {
alert('hehehe');
}
})
};
所以,基本上,这就是发生的事情。整个函数被放入document.ready中。当我单击按钮(该函数绑定(bind)到该按钮)时,if
语句返回 true
,并且代码被执行。它还将状态设置为1
。
状态设置为 1
后,else if
语句应生效并显示 alert("hehehe")
,但事实并非如此。
谁能给我一些建议吗?我做错了什么?
最佳答案
您已在第一个 if
语句中将 "click"
事件绑定(bind)到 "elementToBeClicked"
。
尝试在 if
语句末尾取消绑定(bind)
它,然后看看会发生什么,但通常只是为了检查 else
是否正在执行,只需将警报置于 click
事件之外即可。
关于Javascript else 条件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15454490/