@ZEAL Blog·厉
We stand alone,
TOGETHER.

getElementsByClass

Posted by zeal on 2005-11-24 16:48 , 1504 characters |  + 2 - 0   English
转载请保留本行原始出处声明信息 : http://www.zeali.net/entry/242 MaDe1nZEAL
标签 ( WebDev ):  , 

获取XHTML页面上所有具有相同className的Element对象的javascript函数:

/**
* rootNode          the root node to be checked on,
*                   for example 'document'
* classToSearch     className to be matched
* tagName           elements name to be searched between,
*                   '*' for all elements in 'rootNode'
* returns           an array contains all elements matched

*/
function getElementsByClass(rootNode,classToSearch,tagName) {
    var elementsToReturn = new Array();

    var elementList = rootNode.getElementsByTagName(tagName);
    var nLen = elementList.length;

    var pattern = new RegExp("\\b"+classToSearch+"\\b");

    for(var i = 0; i < nLen; i++){
        if( pattern.test(elementList[i].className) ){
            elementsToReturn[elementsToReturn.length]
= elementList[i];
        }
    }

    return elementsToReturn;
}

Last Modified on 2007-05-28 10:48
没有评论.
Since 2005.04.27  梦想 就像鸡蛋 要么孵化 要么臭掉RSS Feed (Entries) | Back To Home | @ZEAL | 沪ICP备05024379号