javascript单选框,多选框美化代码_表单特效
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript单选框,多选框美化代码_表单特效,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4848字,纯文字阅读大概需要7分钟。
内容图文
![javascript单选框,多选框美化代码_表单特效](/upload/InfoBanner/zyjiaocheng/308/3c0e66c79f594d6aac6fdaed6aaa4d65.jpg)
crir = {
init: function() {
arrLabels = document.getElementsByTagName('label');
searchLabels:
for (var i=0; i // get the input element based on the for attribute of the label tag
if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value != '') {
labelElementFor = arrLabels[i].getAttributeNode('for').value;
inputElement = document.getElementById(labelElementFor);
}
else {
continue searchLabels;
}
inputElementClass = inputElement.className;
// if the input is specified to be hidden intiate it
if (inputElementClass == 'crirHiddenJS') {
inputElement.className = 'crirHidden';
inputElementType = inputElement.getAttributeNode('type').value;
// add the appropriate event listener to the input element
if (inputElementType == "checkbox") {
inputElement.onclick = crir.toggleCheckboxLabel;
}
else {
inputElement.onclick = crir.toggleRadioLabel;
}
// set the initial label state
if (inputElement.checked) {
if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_checked'}
else { arrLabels[i].className = 'radio_checked' }
}
else {
if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_unchecked'}
else { arrLabels[i].className = 'radio_unchecked' }
}
}
else if (inputElement.nodeName != 'SELECT' && inputElement.getAttributeNode('type').value == 'radio') { // this so even if a radio is not hidden but belongs to a group of hidden radios it will still work.
arrLabels[i].onclick = crir.toggleRadioLabel;
inputElement.onclick = crir.toggleRadioLabel;
}
}
},
findLabel: function (inputElementID) {
arrLabels = document.getElementsByTagName('label');
searchLoop:
for (var i=0; i if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value == inputElementID) {
return arrLabels[i];
break searchLoop;
}
}
},
toggleCheckboxLabel: function () {
labelElement = crir.findLabel(this.getAttributeNode('id').value);
if(labelElement.className == 'checkbox_checked') {
labelElement.className = "checkbox_unchecked";
}
else {
labelElement.className = "checkbox_checked";
}
},
toggleRadioLabel: function () {
clickedLabelElement = crir.findLabel(this.getAttributeNode('id').value);
clickedInputElement = this;
clickedInputElementName = clickedInputElement.getAttributeNode('name').value;
arrInputs = document.getElementsByTagName('input');
// uncheck (label class) all radios in the same group
for (var i=0; i inputElementType = arrInputs[i].getAttributeNode('type').value;
if (inputElementType == 'radio') {
inputElementName = arrInputs[i].getAttributeNode('name').value;
inputElementClass = arrInputs[i].className;
// find radio buttons with the same 'name' as the one we've changed and have a class of chkHidden
// and then set them to unchecked
if (inputElementName == clickedInputElementName && inputElementClass == 'crirHidden') {
inputElementID = arrInputs[i].getAttributeNode('id').value;
labelElement = crir.findLabel(inputElementID);
labelElement.className = 'radio_unchecked';
}
}
}
// if the radio clicked is hidden set the label to checked
if (clickedInputElement.className == 'crirHidden') {
clickedLabelElement.className = 'radio_checked';
}
},
addEvent: function(element, eventType, doFunction, useCapture){
if (element.addEventListener)
{
element.addEventListener(eventType, doFunction, useCapture);
return true;
} else if (element.attachEvent) {
var r = element.attachEvent('on' + eventType, doFunction);
return r;
} else {
element['on' + eventType] = doFunction;
}
}
}
crir.addEvent(window, 'load', crir.init, false);
在线演示http://img.jb51.net/online/checkbox/sample.html
打包下载
内容总结
以上是互联网集市为您收集整理的javascript单选框,多选框美化代码_表单特效全部内容,希望文章能够帮你解决javascript单选框,多选框美化代码_表单特效所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。