您好我正在编写一个程序,当您单击一个div时,它会隐藏它自己的类,并显示另一个类但它不起作用,这是我的代码。
$("#one").click(function(){
var c = name1.length;
if (c % 2 > 0) {
j = 1;
$(".color").hide();
$(".text3").show();
}else if(c % 2 == 0){
j = 0;
$(".color").hide();
$(".text2").show();
}
});
答案 0 :(得分:1)
$("#one").click(function() {
$( this ).toggleClass( "your_class" );
});
答案 1 :(得分:1)
据我所知,代码看起来是正确的。如果没有在网站的背景下看到它,我认为您的问题可能是以下之一:
j
不在范围内答案 2 :(得分:0)
尝试放入document.ready,如果没有它,你的脚本就不会运行。
$( document ).ready(function() {
$( "#one" ).on( "click", function() {
var c = name1.length;
if (c % 2 > 0) {
j = 1;
$(".color").hide();
$(".text3").show();
}else if(c % 2 == 0){
j = 0;
$(".color").hide();
$(".text2").show();
}
});
});
答案 3 :(得分:-1)
我认为你不想显示/隐藏与该类名称不同的差异,而是想要切换div#one的类名。