首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像添加到html类型输入复选框或单选按钮

问将图像添加到html类型输入复选框或单选按钮
EN

Stack Overflow用户
提问于 2011-02-25 20:18:04
回答 5查看 27.9K关注 0票数 3

我正在尝试为我的未选中和选中的html输入类型的值、复选框和单选按钮指定一个图像。

我得到了这个:

代码语言:javascript
复制
background: url("image.png") no-repeat;

但它似乎不工作在单选和复选框按钮。

有没有人知道什么能行得通?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-02-25 20:21:39

看看这个Ryan Fait - Custom checkboxes and radio buttons ..。希望这就是你要找的;)

票数 2
EN

Stack Overflow用户

发布于 2011-03-11 21:53:54

我已经找到了答案,并找到了答案。在这些论坛的帮助下http://forums.digitalpoint.com/showthread.php?t=665980

所有代码如下:您可以复制到单个文件,并添加您自己的图像,它将工作。

代码语言:javascript
复制
<html>
<script type="text/javascript">
//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = '52 0 ROff.png';
var imgTrue = '52 0 ROn.png';

//replace the checkbox with an image and setup events to handle it
function replaceChecks() {
    //get all the input fields on the page
    inputs = document.getElementsByTagName('input');

    //cycle trough the input fields
    for(var i=0; i<inputs.length; i++) {

    //check if the input is a checkbox
    if(inputs[i].getAttribute('type') == 'checkbox') {

        //create a new image
        var img = document.createElement('img');

        //check if the checkbox is checked
        if(inputs[i].checked) {
            img.src = imgTrue;
        } else {
            img.src = imgFalse;
        }

        //set image ID and onclick action
        img.id = 'checkImage'+i;
        //set image
        img.onclick = new Function('checkClick('+i+')');

        //place image in front of the checkbox
        inputs[i].parentNode.insertBefore(img, inputs[i]);

        //hide the checkbox
        inputs[i].style.display='none';
    }
}
}

//change the checkbox status and the replacement image
function checkClick(i) {
if(inputs[i].checked) {
    inputs[i].checked = '';
    document.getElementById('checkImage'+i).src=imgFalse;
} else {
    inputs[i].checked = 'checked';
    document.getElementById('checkImage'+i).src=imgTrue;
}
}

</script>
</html>
<body>
<input type="checkbox" />
<script type="text/javascript">replaceChecks();</script>
</body>
票数 5
EN

Stack Overflow用户

发布于 2014-03-07 20:25:10

在checkbox中,如果你想改变图像或颜色,那么优化的方式是。在这里你可以点击"item name“或"checkbox”来获得效果。

HTML:

代码语言:javascript
复制
  <div class="had">
        <div class="ch">
            <input type="checkbox" id="sho1" name="chk1">
            <div class="so"></div>
            <label for="sho1">Select one</label>
        </div>
        <div class="ch">
            <input type="checkbox" id="sho2" name="chk2">
            <div class="so"></div>
            <label for="sho2">Select two</label>
        </div>
    </div>

CSS:

代码语言:javascript
复制
    input {
               display: none;
    }

        input [type=checkbox]:checked+.so{
              background-image:url('http://goo.gl/3tza1X');
              background-repeat:no-repeat;background-color:green;
              border-radius:50%;background-position: -2px -1px;
    }

        input [type=checkbox]:checked+.so+label{
           color: red;
    }

        .so {
             margin: 2px; float: left;height: 30px;width: 30px;
             border: 3px solid #D8D8D8;background-color: #4679BD;
             border-radius: 5px;
    }

        .show {
               margin-left: 30px;
    }

        .had {
               margin: 50px auto;width: 50%;border: 1px solid black;
               height: 30px;padding:10px;
    }

        .ch {
              float: left;margin-left:20px;width:40%
    }

        label {
               height: 30px;width: 100%;position:relative;
               display:block;margin-top: 5px;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5117116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档