我正在尝试为 html 输入类型复选框和收音机指定要用于我的未选中和选中值的图像。
我有这个:
background: url("image.png") no-repeat;
但它似乎不适用于单选按钮和仅复选框按钮。
有谁知道有用的东西吗?
原文由 f1wade 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在尝试为 html 输入类型复选框和收音机指定要用于我的未选中和选中值的图像。
我有这个:
background: url("image.png") no-repeat;
但它似乎不适用于单选按钮和仅复选框按钮。
有谁知道有用的东西吗?
原文由 f1wade 发布,翻译遵循 CC BY-SA 4.0 许可协议
我找到并找到了答案。在这些论坛的帮助下 http://forums.digitalpoint.com/showthread.php?t=665980
所有代码都在下面:您可以复制到一个文件并添加您自己的图像,它会起作用。
<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>
原文由 f1wade 发布,翻译遵循 CC BY-SA 2.5 许可协议
1 回答1k 阅读✓ 已解决
2 回答1.5k 阅读✓ 已解决
2 回答893 阅读✓ 已解决
1 回答1.1k 阅读✓ 已解决
1 回答904 阅读✓ 已解决
2 回答787 阅读
1 回答775 阅读✓ 已解决
看看这个 Ryan Fait - 自定义复选框和单选按钮……希望它是你正在寻找的;)