[英]jquery toggle not working as expected
Hello people here is my JQUERY code below... 大家好,这是我的下面的JQUERY代码...
$(function(){
$(".block2").toggle(
function(){$(this).text(1);},
function(){$(this).text(0);});
});
where block2
is the same class name for 2 divs...what iam trying to do is if i click first div
it should enter 1
in it.. If i click second div(second time)
it should enter 0
how to fix this?? 其中
block2
是2 divs的同一个类名...我想做的是,如果我单击first div
,则应在其中输入1
如果我单击second div(second time)
,则应输入0
如何解决此问题? ?
Try this, 尝试这个,
$(function(){
$('#first_div, #second_div').on('click',function(){
$(".block2").text(function(){
return $(this).text()==1 ? 0 : 1
});
});
});
Updated with same class block2
更新为同一类
block2
$(function(){
$('.block2').on('click',function(){
$(this).text(function(){
return $(this).text()==0 ? 1 : 0;
});
});
});
This is the "click an element to run the specified functions" signature of .toggle(). 这是.toggle()的“单击元素以运行指定的功能”签名。 It should not be confused with the "change the visibility of an element" of .toggle() which is not deprecated.
不应将它与不建议使用的.toggle()的“更改元素的可见性”相混淆。 The former is being removed to reduce confusion and improve the potential for modularity in the library.
删除了前者,以减少混乱并提高库中模块化的可能性。 The jQuery Migrate plugin can be used to restore the functionality.
jQuery Migrate插件可用于恢复功能。 You can try this
你可以试试这个
$(function(){
$(".block2").click(function
if($(this).hasClass("clicked")) {
$(this).text(1);
$(this).removeClass("clicked");
} else {
$(this).text(1);
$(this).addClass("clicked");
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.