我有一个问题,当我点击链接p打开,然后我点击另一个链接,它不起作用。当我再次单击时,它会起作用。
这是我的jQuery代码:
var toggle = 0;
$(document).ready(function () {
$(".feedback_block").each(function () {
$("a", this).click(function (e) {
if (toggle == 0) {
$(this).parent().children("p").stop(true, true).fadeIn(500);
$(this).addClass("clicked");
$(this).children().addClass("clicked_span");
toggle = 1;
console.log(toggle);
} else
if (toggle == 1) {
$(this).parent().children("p").stop(true, true).fadeOut(500);
$(this).removeClass("clicked");
$(this).children().removeClass("clicked_span");
toggle = 0;
console.log(toggle);
}
e.stopPropagation();
return false;
});
toggle = 0;
});
});当我单击参数切换get 1时,当我单击另一个链接时,初始值应该是0。我该怎么做呢?
我的例子:http://jsfiddle.net/amkrtchyan/tjzaR/
发布于 2012-03-20 18:27:57
您可以简单地使用toggle() (http://jsfiddle.net/tjzaR/2/)
$(document).ready(function () {
$(".feedback_block").each(function () {
$("a", this).toggle(function (e) {
$(this).parent().children("p").stop(true, true).fadeIn(500);
$(this).addClass("clicked");
$(this).children().addClass("clicked_span");
}, function(){
$(this).parent().children("p").stop(true, true).fadeOut(500);
$(this).removeClass("clicked");
$(this).children().removeClass("clicked_span");
});
});
});或者你可以这样做(在这里拉小提琴http://jsfiddle.net/tjzaR/1/)
$(".feedback_block").each(function () {
$("a", this).click(function (e) {
if (!$(this).parent().children("p").is(":visible")){
$(this).parent().children("p").stop(true, true).fadeIn(500);
$(this).addClass("clicked");
$(this).children().addClass("clicked_span");
} else{
$(this).parent().children("p").stop(true, true).fadeOut(500);
$(this).removeClass("clicked");
$(this).children().removeClass("clicked_span");
}
return false;
});
});发布于 2012-03-20 18:31:13
$("a", this).toggle(function () {
$(this).parent().children("p").stop(true, true).fadeIn(500);
$(this).addClass("clicked");
$(this).children().addClass("clicked_span");
return false;
}, function(){
$(this).parent().children("p").stop(true, true).fadeOut(500);
$(this).removeClass("clicked");
$(this).children().removeClass("clicked_span");
return false;
});我们能不能不直接使用切换功能。Toggle
https://stackoverflow.com/questions/9784936
复制相似问题