JS实现⼀个按钮的⽅法本⽂实例讲述了JS实现⼀个按钮的⽅法。分享给⼤家供⼤家参考。具体实现⽅法如下:<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="edge">
<script>
var btn = new Btn();
btn.init({width:300});
bindEvent(btn,'show',function(){
alert(1);
})
bindEvent(btn,'click',function(){
alert(2);
})
var oBtn = ElementById('btn');
fireEvent(btn,'show');
}
}
function Btn(){
this.btn= null;
this.settings = {
width:200,
height:40,
borderRadius:6,
text :'按钮'
};
}
Btn.prototype.init = function (opt){
extend(this.settings,opt);
}
at = function (){
this.btn =ateElement('div');
document.body.appendChild(this.btn);
this.btn.innerHTML = ;
this.setData();
}
Btn.prototype.destory = function (){
veChild(this.btn);
}
Btn.prototype.setData = function (){
this.btn.style.width = this.settings.width +'px';
this.btn.style.height = this.settings.height +'px';
this.btn.style.border ='solid #f00 '+ this.settings.borderRadius +'px';
}
function extend(obj1,obj2){
for(var attr in obj2){
obj1[attr] = obj2[attr];
}
}
function bindEvent(obj,events,fn){
obj.listeners = obj.listeners || {};
obj.listeners[events] = obj.listeners[events] || [];
obj.listeners[events].push( fn );
deType){
if(obj.addEventListener){
obj.addEventListener(events,fn,false);
}else{
obj.attachEvent('on'+events,fn);
}
}
}
function fireEvent (obj,events){
if(obj.listeners[events]){
js教程removechild
for(var i in obj.listeners[events]){
obj.listeners[events][i]();
}
}
}
</script>
</head>
<body>
<a id="btn" >12</a>
</body>
</html>
希望本⽂所述对⼤家的javascript程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。