Element.prototype = {
hasClass : function(className){
return this.className.contains(className, ' ');
},
getId: function(){
return this.id;
}
};
单独运作
String.prototype.contains = function(item, from){
return this.indexOf(item, from) != -1;
};
Element.prototype.hasClass = function(className){
return this.className.contains(className, ' ');
};
Element.prototype.getId = function() {
return this.id;
};
document.body.innerHTML=document.getElementById('foo').hasClass('blah');

<div id="foo" class="blah"></div>
&#13;
这是我第一次处理prototype
答案 0 :(得分:1)
我认为问题在于您将原型设置为新对象。 如果您使用的是jQuery或类似的东西,那么您可以这样做:
$.extend(Element.prototype, {
hasClass : function(className){
return this.className.contains(className, ' ');
},
getId: function(){
return this.id;
}
});
那应该有用。