我有一堆 div:
<div class='option'>
<span>here is a label for one pair of radio buttons</span>
<a href='#' rel='radio' class='on'>On</a>
<a href='#' rel='radio' class='off'>Off</a>
</div>
我正在尝试使用 jQuery 在这两个类(“off”和“on”)之间切换。
<script>;
$(document).ready(function(){
$("a[rel='radio']").click(function(){
if($(this).attr('class')=='on'){
//make $(this) have class 'off'
//make the other a in *this* div have class 'on'
} else {
//make $(this) have class 'on'
//make the other a in the div have class 'off'
}
});
});
</script>
如何将注释的伪代码转换为现实世界的 jQuery?
最佳答案
您描述的行为与标准单选按钮不同,但可以如下所示:http://jsfiddle.net/BJGKH/ .
$("a[rel='radio']").click(function(){
// this and sibling - toggle both classes from each
$(this).siblings("a[rel='radio']").addBack().toggleClass("on off");
});
关于jquery - 使用 jquery 的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12167305/