JS面向对象之单选框的实现方法

这篇文章主要介绍JS面向对象之单选框的实现方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

本文实例为大家分享了JS面向对象之单选框实现代码,供大家参考,具体内容如下

描述:

JS面向对象——单选框的实现

效果:

实现:

Utile.js

(function(){
Object.prototype.addProto=function(sourceObj){
varnames=Object.getOwnPropertyNames(sourceObj);
for(vari=0;i0){
fileName+=key+".png";
break;
}
}
if(arr.length===0){
returnfalse;
}else{
return{
file:fileName,
w:arr[0].frame.w,
h:arr[0].frame.h,
x:arr[0].frame.x,
y:arr[0].frame.y
};
}
},
getNameXMLData:function(name){
varfileName=RES.basePath;
for(varkeyinlist){
varelem=list[key].querySelector("[n="+name+"]");
if(elem){
fileName+=list[key].getAttribute("imagePath");
break;
}
}
if(!elem)returnfalse;
return{
file:fileName,
w:elem.getAttribute("w"),
h:elem.getAttribute("h"),
x:elem.getAttribute("x"),
y:elem.getAttribute("y")
}
},
getImage:function(name){
varobj;
if(RES.type==="json"){
obj=RES.getNameJSONData(name);
}elseif(RES.type==="xml"){
obj=RES.getNameXMLData(name)
}
if(!obj)return;
vardiv=document.createElement("div");
Object.assign(div.style,{
width:obj.w+"px",
height:obj.h+"px",
backgroundImage:"url("+obj.file+")",
backgroundPositionX:-obj.x+"px",
backgroundPositionY:-obj.y+"px",
position:"absolute"
});
returndiv;
},
changeImg:function(elem,name){
varobj;
if(RES.type==="json"){
obj=RES.getNameJSONData(name);
}elseif(RES.type==="xml"){
obj=RES.getNameXMLData(name)
}
if(!obj)return;
Object.assign(elem.style,{
width:obj.w+"px",
height:obj.h+"px",
backgroundImage:"url("+obj.file+")",
backgroundPositionX:-obj.x+"px",
backgroundPositionY:-obj.y+"px",
position:"absolute"
});
}
}
})();

UIComponent.js

varCheckBox=(function(){
functionCheckBox(parent){
this.checkView=this.init(parent);
}
/*
//ES5单例
CheckBox.getInstance=function(){
if(!CheckBox._instance){
CheckBox._instance=newCheckBox();
}
returnCheckBox._instance;
};*/
CheckBox.prototype.addProto({
_label:"",
_checked:false,
init:function(parent){
if(this.checkView)returnthis.checkView;
vardiv=document.createElement("div");
varicon=RES.getImage("f-checkbox");
div.appendChild(icon);
varlabel=document.createElement("span");
div.style.position=icon.style.position=label.style.position="relative";
icon.style.float=label.style.float="left";
label.textContent="";
Object.assign(label.style,{
fontSize:"16px",
lineHeight:"20px",
marginLeft:"5px",
marginRight:"10px"
});
varh=RES.getNameXMLData("f-checkbox").h;
icon.style.top=(20-h)/2+"px";
div.appendChild(label);
parent.appendChild(div);
this.clickHandlerBind=this.clickHandler.bind(this);
div.addEventListener("click",this.clickHandlerBind);
returndiv;
},
clickHandler:function(e){
this.checked=!this.checked;
},
setlabel(value){
this._label=value;
this.checkView.lastElementChild.textContent=value;
},
getlabel(){
returnthis._label;
},
setchecked(value){
if(this._checked===value)return;
this._checked=value;
if(value){
RES.changeImg(this.checkView.firstElementChild,"f-checkbox-active");
}else{
RES.changeImg(this.checkView.firstElementChild,"f-checkbox");
}
this.checkView.firstElementChild.style.position="relative";
this.dispatchMessage(value);
},
dispatchMessage:function(value){
varevt=newEvent("change");
evt.checked=value;
evt.elem=this;
document.dispatchEvent(evt);
},
getchecked(){
returnthis._checked;
}
});
returnCheckBox;
})();

varRadio=(function(){
functionRadio(parent,groupName){
this.constructor.supClass.constructor.call(this,parent);
this.groupName=groupName;
this.checkView.self=this;
this.checkView.setAttribute("groupName",groupName);
}

Radio.extendClass(CheckBox);

Radio.prototype.addProto({
clickHandler:function(e){
//console.log(Model.instance.menuData);
if(this.checked)return;
varlist=document.querySelectorAll("[groupName="+this.groupName+"]");
for(vari=0;i

html





Title








以上是“JS面向对象之单选框的实现方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2021-05-30 14:05:48
分享
海报
173
上一篇:C#通过rabbitmq如何实现定时任务下一篇:Python如何实现自定义计算时间过滤器
目录

    忘记密码?

    图形验证码