...utilizzando un commento condizionale
breve spiegazione:codice:/*@cc_on Element = function () {}; Element.prototype.getAttribute = function (attribute) { switch(attribute){ case "class": attribute = "className";break; case "for": attribute = "htmlFor";break; case "style": return this.style.cssText;break; case "type":return(this.id)?document.getElementById(this.id).type:this.type;break; case "accesskey":return this.accessKey;break; case "maxlength":return this.maxLength;break; } return this[attribute]; } Element.prototype.setAttribute = function (attribute, value) { var interface = new Element; switch(attribute){ case "class": attribute = "className";break; case "for": attribute = "htmlFor";break; case "type": var me=this.parentNode; if(!/id=/.test(this.outerHTML)) this.id=this.uniqueID; this.outerHTML=this.outerHTML.replace(/type=[a-zA-Z]+/," ").replace(">"," type='"+value+"'>"); var t=me.childNodes; var max=t.length; for(var i=0;i<max;i++) if(t[i].id==this.id){ t[i].getAttribute=interface.getAttribute; t[i].setAttribute=interface.setAttribute; } return;break; case "accesskey":this.accessKey=value;return;break; case "style": this.style.cssText =value;return;break; case "maxlength":this.maxLength=value;return;break; } if(value.indexOf("on")!=0) this[attribute] = function(){eval(value)}; else this[attribute] = value; } var __IEcreateElement = document.createElement; document.createElement = function (tagName) { var element = __IEcreateElement(tagName); element.getAttribute=interface.getAttribute; element.setAttribute=interface.setAttribute; return element; } var interface = new Element; onload=function(){ var list=document.all; var max=list.length; while(--max) { list[max].getAttribute=interface.getAttribute; list[max].setAttribute=interface.setAttribute; } //HTML node list[0].getAttribute=interface.getAttribute; list[0].setAttribute=interface.setAttribute; } @*/
creiamo un interfaccia Element da cui estenderemo tutti i nodicodice:Element = function () {};
aggiungiamo la nuova getAttribute,codice:Element.prototype.getAttribute = function (attribute) { switch(attribute){ case "class": attribute = "className";break; case "for": attribute = "htmlFor";break; case "style": return this.style.cssText;break; case "type":return(this.id)?document.getElementById(this.id).type:this.type;break; case "accesskey":return this.accessKey;break; case "maxlength":return this.maxLength;break; } return this[attribute]; }
con supporto per class,for,style,type,accesskey,maxlength
aggiungiamo la nuova setAttribute,codice:Element.prototype.setAttribute = function (attribute, value) { var interface = new Element; switch(attribute){ case "class": attribute = "className";break; case "for": attribute = "htmlFor";break; case "type": var me=this.parentNode; if(!/id=/.test(this.outerHTML)) this.id=this.uniqueID; this.outerHTML=this.outerHTML.replace(/type=[a-zA-Z]+/," ").replace(">"," type='"+value+"'>"); var t=me.childNodes; var max=t.length; for(var i=0;i<max;i++) if(t[i].id==this.id){ t[i].getAttribute=interface.getAttribute; t[i].setAttribute=interface.setAttribute; } return;break; case "accesskey":this.accessKey=value;return;break; case "style": this.style.cssText =value;return;break; case "maxlength":this.maxLength=value;return;break; } if(value.indexOf("on")!=0) this[attribute] = function(){eval(value)}; else this[attribute] = value; }
con supporto per class,for,style,type,accesskey,maxlength e per gli eventi onclick,ecc...
estendiamo la createElement in maniera da supportare gli standard.codice:var __IEcreateElement = document.createElement; document.createElement = function (tagName) { var element = __IEcreateElement(tagName); element.getAttribute=interface.getAttribute; element.setAttribute=interface.setAttribute; return element; }
ora tutti i nodi creati a runtime avranno un comportamento standard,
manca pero' il supporto per i nodi presenti nell'HTML,aggiungiamoglielo.
codice:var interface = new Element; onload=function(){ var list=document.all; var max=list.length; while(--max) { list[max].getAttribute=interface.getAttribute; list[max].setAttribute=interface.setAttribute; } //HTML node list[0].getAttribute=interface.getAttribute; list[0].setAttribute=interface.setAttribute; }![]()
ora abbiamo il supporto completo w3c, o almeno piu attinente allo standard
non funziona sui nodi creati con innerHTML/outerHTML, si puo estendere innerHTML cosi' come e' stato fatto ma non e' mia intenzione poiche innerHTML non e' standard
c'e' un altro modo di fare l'ultimo step(aggiungere il supporto ai nodi già creati dall'HTML ),
usare un file HTC come viene fatto QUI


Rispondi quotando