【发布时间】:2017-10-01 21:45:42
【问题描述】:
$('#axe, #saw').click(function(e) {
var id = $(this).attr('id');
if (id == 'axe') {
$(this).toggleClass('as-in-one as-one');
} else if (id == 'saw') {
$(this).toggleClass('as-in-two as-two');
}
})
.as-in-one {
color: red;
}
.as-one {
color: blue;
}
.as-in-two {
color: red;
}
.as-two {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='axe' class='axe as-in-one'>AXE</button>
<button id='saw' class='saw as-in-two'>SAW</button>
在我的代码中,我希望能够只更改一个按钮的类,从 red 到 blue,但是如果我点击另一个按钮,第一个按钮会变成默认类 red,如果它是blue 和另一个切换到 blue 反之亦然。
【问题讨论】:
-
对变量的看法。如果单击并将按钮更改为蓝色,请将其设置为全局变量,然后单击检查是否为蓝色,然后设置为蓝色。
-
"...但当时只有一个" 呃,单选按钮就是这样工作的。
标签: javascript jquery html css jquery-selectors