更改复选框检查图像到自定义图像

the*_*mhz 34 html css xhtml

我正在尝试使用css更改复选框的默认"框图像",但它无法正常工作.这有什么办法吗?

.class_checkbox{
    background: url("../images/button_bullet_normal.png") no-repeat scroll 0 0 transparent;
}
Run Code Online (Sandbox Code Playgroud)

在我做的类文件中

<input type="checkbox" class="class_checkbox">
Run Code Online (Sandbox Code Playgroud)

小智 90

你可以使用纯css,只需在复选框中添加一个标签,如下所示:

.check_box {
    display:none;
}

.check_box + label{
    background:url('images/check-box.png') no-repeat;
    height: 16px;
    width: 16px;
    display:inline-block;
    padding: 0 0 0 0px;
}

.check_box:checked + label{
    background:url('images/check-box-checked.png') no-repeat;
    height: 16px;
    width: 16px;
    display:inline-block;
    padding: 0 0 0 0px;
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨!我想知道相应的HTML代码是什么?是<input type ="checkbox"id ="chB1"class ="check_box"/> <label for ="chB1">这是标签</ label>? (4认同)

iam*_*eed 11

尝试:

<input type="checkbox" class="input_class_checkbox">
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('.input_class_checkbox').each(function(){
    $(this).hide().after('<div class="class_checkbox" />');

});

$('.class_checkbox').on('click',function(){
    $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
});
Run Code Online (Sandbox Code Playgroud)

小提琴:http: //jsfiddle.net/cn6kn/

  • @AamirAfridi看到我刚才添加的小提琴.代码是对的.代码检查CSS复选框是否具有类`checked`,如果确实如此,它将复选框上的checked属性设置为true. (3认同)

Ton*_*ony 7

我使用纯CSS创建了一个小提琴.投票最多的答案不会处理点击事件,也不会有效,因为复选框值不会更改.

这是我的例子:

http://jsfiddle.net/kEHGN/1/

基本上,我们需要以下html:

<div class="checkbox_wrapper">
    <input type="checkbox" />
    <label></label>
</div>
Run Code Online (Sandbox Code Playgroud)

以下CSS:

.checkbox_wrapper{
    position: relative;
    height: 16px;
    width: 17px;
}

input[type="checkbox"] {
    opacity:0;
    height: 16px;
    width: 17px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
}

input[type="checkbox"] + label{
    background:url('../images/unchecked.png') no-repeat;
    height: 16px;
    width: 17px;
    display:inline-block;
    padding: 0 0 0 0px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
}

input[type="checkbox"]:checked + label{
    background:url('../images/checked.png') no-repeat;
    height: 16px;
    width: 17px;
    display:inline-block;
    padding: 0 0 0 0px;
}
Run Code Online (Sandbox Code Playgroud)

只需检查图像的路线,宽度和高度应等于图像的宽度和高度.在小提琴手上我使用base64编码的图像.

我希望它有用.