venerdì, maggio 05, 2006

getElementByClass

this code is now obsolete, take a look at this one to a better one, thanks.


document.getElementsByClassName = function(searchClass,node,tag)
{

if(node == null)node=document;
var ce = new Array();
if(tag==null || tag=='*')tag='*';
var els = new Array();
if (tag=='*' && document.evaluate){
var xpr=document.evaluate("//*",document, null, 0, null);
var t=true;
while (t=xpr.iterateNext()){
if(els.push)
els.push(t);
else
els[els.length]=t;
};
}
else
els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
var i;var j;
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) )
if (ce.push)
ce.push(els[i]);
else
ce[j++] = els[i];
}

return ce;

}
Posta un commento