Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中更改dbclick上的单选按钮的原始颜色_Javascript_Jquery_Button_Radio - Fatal编程技术网

Javascript 如何在jquery中更改dbclick上的单选按钮的原始颜色

Javascript 如何在jquery中更改dbclick上的单选按钮的原始颜色,javascript,jquery,button,radio,Javascript,Jquery,Button,Radio,若我点击单选按钮,颜色变为黄色,但若我双击它,我想要它的原始颜色,即灰色。 我该怎么做 JS $(document).ready(function() { $('input:radio').change(function() { var $this = $(this); $this.closest('.box').find('div.highlight').removeClass('highlight'); $this.closest('.

若我点击单选按钮,颜色变为黄色,但若我双击它,我想要它的原始颜色,即灰色。 我该怎么做

JS

$(document).ready(function() {
    $('input:radio').change(function() {
        var $this = $(this);
        $this.closest('.box').find('div.highlight').removeClass('highlight');
        $this.closest('.q').addClass('highlight');
    });
});
CSS

.q {
    background: #ccc;
    margin: 10 px;
}

.highlight {
    background: #FC0;
}
HTML

<div class="box">
    <div class="q">
        <input type="radio" id="1" name="11" />
        <label for 1>Radio Button1</label>
    </div>
    <div class="q">
        <input type="radio" id="2" name="11" /`enter code here`>
        <label for 1>Radio Button2</label>
    </div>
</div>

收音机按钮1
收音机按钮2

使用dblclick并单击事件

<script>
$(document).ready(function(){
    $('input:radio').click(function(){
        var $this = $(this);
        $this.closest('.box').find('div.highlight').removeClass('highlight');
        $this.closest('.q').addClass('highlight');
    })
    .dblclick(function(){
        var $this = $(this);
        $this.closest('.box').find('div.highlight').removeClass('highlight');
        $this.closest('.q').removeClass('highlight');
    });
});
</script>

$(文档).ready(函数(){
$('input:radio')。单击(函数(){
var$this=$(this);
$this.closest('.box').find('div.highlight').removeClass('highlight');
$this.closest('.q').addClass('highlight');
})
.dblclick(函数(){
var$this=$(this);
$this.closest('.box').find('div.highlight').removeClass('highlight');
$this.closest('.q').removeClass('highlight');
});
});

为什么不使用
toggleClass
?也可以使用toggleClass:)