因为ie8之下的浏览器不支持Function.prototype.bind,所以要进行重写
html代码:
<input type="button" value="Click me" id="btn">
<span id="text">我会变色</span>
js代码:
if(!Function.prototype.bind){
Function.prototype.bind = function(context){
var args = Array.prototype.slice.call(arguments),
self = this;
return function(){
console.log(context);context的值<span id="text" color="blue">我会变色</span>
console.log(self);self就是function(color){this.style.color = color || "red";}
return self.apply(context,args.slice(1));
}
}
}
var btn = document.getElementById("btn"),
text = document.getElementById("text");
btn.onclick = function(color){
this.style.color = color || "red";
}.bind(text,"blue");