可以改变对象颜色的插件,面向对象应用

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>改变颜色的插件</title>
<style>
.box{
100px;
height:100px;
background:blue;
}
</style>
<script>
window.onload=function(){
var oBtn = document.getElementById('btn');
var oDiv = document.getElementById('box');

function Tored(oBtn,oDiv,color){
this.btn=oBtn;
this.div=oDiv;
this.color=color;
}
Tored.prototype.click=function(){
var a = this;
this.btn.onclick=function(){
a.reds();//这里的this是btn,他身上没有reds这个方法
};
}
Tored.prototype.reds=function(){
this.div.style.background=this.color;
};

var otred = new Tored(oBtn,oDiv,'#000');
otred.click();
};
</script>
</head>

<body>
<input type="button" value="变红" id="btn">
<div class="box" id="box"></div>
</body>
</html>

原文地址:https://www.cnblogs.com/wangjie-001/p/6056553.html