js 模拟getAttribute()
的有关信息介绍如下:在dom对象中可以自建属性,但edge中不支持dom中非标准属性的点格式(obj.attribute)的访问,即必须使用obj.getAttribute(attribute)。当一个函数的参数中既可以是dom对象,也可以是自建对象,且要处理的属性名相同时(dom中为非标属性)就会出现问题。用点格式访问自建对象没问题,访问dom对象则失败;反之用getAttribute()访问dom没问题,但访问自建对象则错误(自建对象没有定义该函数)。为此必须为自建对象建立一个getAttribute()方法来解决以上问题。本来想偷懒从百度找,可惜都是对getAttribute()的使用介绍,或者挂在dom原型之下……
想了一下遍历对象的结果,随手写下:
getAttribute:function(attribute){
for(i in this)
if(i==attribute) return this[i];
return "undefined";
},
将上述过程加入自建对象即可。
同理有:
setAttribute:function(attribute,value){
for(i in this)
if(i==attribute){
this[i]=value;
return true;
}
return false;
}