写一个原型继承的例子
程序员文章站
2022-06-15 15:57:42
...
function Elem(id) {
this.elem = document.getElementById(id);
}
Elem.prototype.html = function(val){
var elem = this.elem;
if(val) {
elem.innerHTML = val;
return this;
}else {
return elem.innerHTML;
}
}
Elem.prototype.on = function(type,fn){
var elem = this.elem;
elem.addEventListener(type,fn);
}
var div1 = new Elem('div');
console.log(div1.html());
div1.html('<p>假装自己是一段文字</p>').on('click', function(){
alert('clicked')
}
);