首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

Object.prototype的有关问题

2012-03-21 
Object.prototype的问题Object.prototype.getfunction(att){returnthis.getAttribute(att)}varobjdocum

Object.prototype的问题
Object.prototype.get   =   function(att){
return   this.getAttribute(att);
}
var   obj=document.getElementById( "img ");
alert(obj.get( "src "));
在FF下正常,在IE里确不对了。
没用过prototype,谁能说说

[解决办法]
任何一个html元素的typeof都是object,但是你给Object.prototype下面挂载的属性方法不会挂到html元素下
HTMLElement.prototype.test=function(){alert(1)}
document.getElementsByTagName( "body ")[0].test()
上面代码在ff下工作,ie保护了HTMLElement对象,并且ie下压根没有HTMLElement对象

Object.prototype.test=function(){alert(1)};
document.getElementsByTagName( "html ")[0].test();
上面代码只能在ff下运行,而ie会报没有该方法

事实上html元素后面挂prototype可以
var e=new function(){this.test=function(){alert(1)};};
document.getElementsByTagName( "html ")[0].prototype=e
document.getElementsByTagName( "html ")[0].prototype.test();

ie会保护html元素,所有的DOM元素虽然类型也是object但是并不继承自Object对象

热点排行