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;

}

4 commenti:

DucPetit ha detto...

oh che opportuno questo post !
mi chiedevo infatti se esistesse l'operatore getElementByclass :)

Anonimo ha detto...

* Author: Dustin Diaz - http://www.dustindiaz.com */

kentaromiura ha detto...

no, actually dustin diaz is different
this post was only a reminder to me to the syntax of document.evaluate.

abhi ha detto...

check out:
http://pitfalls.wordpress.com/2008/07/07/querying-it-jquery-way-getelementsbyclass/