利用JavaScript更改input中radio和checkbox样式 时间:2020-05-12 人气:1 本文涉及到的图片 <script language="javascript" type="text/javascript"> function getid(id) { return document.getElementById(id); } function gettag(tag, obj) { if (obj) { return obj.getElementsByTagName(tag) } else { return document.getElementsByTagName(tag) } } function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != "function") { window.onload = func; } else { window.onload = function() { oldonload(); func(); } }; } function radio_style() {//3设置样式 if (gettag("input")) { var r = gettag("input"); function select_element(obj, type) {//1设置背景图片 obj.parentNode.style.background = "url(/upload/2009-11/20091124021750277.gif) no-repeat -15px 0px"; if (obj.type == "checkbox") { obj.parentNode.style.background = "url(/upload/2009-11/20091124021750277.gif) no-repeat -15px -15px"; } if (type) { obj.parentNode.style.background = "url(/upload/2009-11/20091124021750277.gif) no-repeat 0px 0px"; if (obj.type == "checkbox") { obj.parentNode.style.background = "url(/upload/2009-11/20091124021750277.gif) no-repeat 0px -15px"; } } } //1 for (var i = 0; i < r.length; i++) { if (r[i].type == "radio" || r[i].type == "checkbox") { r[i].style.opacity = 0; r[i].style.filter = "alpha(opacity=0)";//隐藏真实的checkbox和radio r[i].onclick = function() { select_element(this); unfocus(); } if (r[i].checked == true) { select_element(r[i]); } else { select_element(r[i], 1); } } } function unfocus() {//2处理未选中 for (var i = 0; i < r.length; i++) { if (r[i].type == "radio" || r[i].type == "checkbox") { if (r[i].checked == false) { select_element(r[i], 1) } } } } //2 } } //3 function addevent() { gettag("input", getid("button"))[0].onclick = function() { this.value = "去除样式"; this.onclick = removeevent; radio_style(); }; } function removeevent() { var r = gettag("input"); for (var i = 0; i < r.length; i++) { if (r[i].type == "radio" || r[i].type == "checkbox") { r[i].style.opacity = 100; r[i].style.filter = "alpha(opacity=100)"; r[i].onclick = function() { } r[i].parentNode.style.background = "none"; } } gettag("input", getid("button"))[0].value = "加载样式"; gettag("input", getid("button"))[0].onclick = function() { this.value = "去除样式"; this.onclick = removeevent; radio_style(); }; } addLoadEvent(addevent); </script> 利用javascript自定义radio及checkbox样式 单选框样式: 男 女 boy girl 复选框样式: xhtml CSS Javascript Flash Ajax AS Flex PHP/.NET/JSP 了解 一般 熟练 精通 资深专家 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 加载全部内容