Ho ripescato dalla rete questo script, che permette di prendere un elemento dal valore di un attributo.
Ad esempio:
getElementsByAttribute(document, "*", "title", "mio titolo");
prende tutti i tag della pagina che hanno title impostato a "mio titolo".
Però non sono riuscito a capire come modificare le proprietà di questi elementi...
Ho provato così ma non va:
getElementsByAttribute(document, "*", "title", "mio titolo")[0].setAttribute("href", "http://www.html.it");
Posto il codice dello script
e un file html per provarecodice:function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){ var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName); var arrReturnElements = new Array(); var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null; var oCurrent; var oAttribute; for(var i=0; i<arrElements.length; i++){ oCurrent = arrElements[i]; oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName); if(typeof oAttribute == "string" && oAttribute.length > 0){ if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){ arrReturnElements.push(oCurrent); } } } return arrReturnElements; } // --- if(typeof Array.prototype.push != "function"){ Array.prototype.push = ArrayPush; function ArrayPush(value){ this[this.length] = value; } } // ---
codice:<html> <head> <script src="getElementsByAttribute.js" type="text/javascript"></script> <script type="text/javascript"> mmmvar=new Array(); getElementsByAttribute(document, "*", "title", "mio titolo")[0].setAttribute("href", "http://www.html.it"); </script> </head> <body> aaa abc </body> </html>

Rispondi quotando