2012-10-11 40 views
0

我使用this作为自定义单选按钮和复选框,但我需要在单击时更改:before元素的背景。可能吗?更改CSS选择器与jQuery之前的背景位置

HTML

<div class="checkbox"> 
<input type="checkbox" value="1" name="test"> 
</div> 

CSS(其.LESS)

.checkbox { 
    display: inline-block; 
    margin: 0 4px 0 0; 
    cursor: pointer; 
    text-align: left; 
    padding: 0px; 

    input { display: none; } 

    &:before { 
     background: url(/img/checkboxes.png) no-repeat; 
     content: ""; 
     float: left; 
     height: 18px; 
     width: 19px; 
     margin: 0; 
    } 
} 
+0

你的CSS有一些错误。你不能把输入&:放入.checkbox之前。它需要成为它自己的一类。 –

+0

@ComputerArts它实际上是'less'编译为'css' –

+0

指出!错过了那部分!抱歉! –

回答

2

您不能选择使用jQuery伪元素,我想说的干净的解决方案是操作类的元素使用jQuery:

.checkbox { 
    display: inline-block; 
    margin: 0 4px 0 0; 
    cursor: pointer; 
    text-align: left; 
    padding: 0px; 

    input { display: none; } 

    &:before { 
     background: url(/img/checkboxes.png) no-repeat; 
     content: ""; 
     float: left; 
     height: 18px; 
     width: 19px; 
     margin: 0; 
    } 

    &.someclass:before { 
     background-position:0 -55px; 
    } 
} 

然后:

$('.checkbox').addClass('someclass'); 
+1

就像在这个jsFiddle:http://jsfiddle.net/mori57/S5McL /(这是一个整洁的方式来修改:before,infensus!) –

+0

不错,这有帮助!谢谢! – Stefanie