Javascript隐藏/显示类问题

时间:2011-07-28 19:54:41

标签: javascript class

我有以下函数可以获取具有给定类的文档中的所有元素:

function getElementByClass(objClass)
{
//  This function is similar to 'getElementByID' since there is no inherent function to     get an element by it's class
    var elements = (ie) ? document.all : document.getElementsByTagName('*');
    for (i=0; i<elements.length; i++)
{
    alert(elements[i].className);
    alert(objClass);
    if (elements[i].className==objClass)
    {
        return elements[i]
    }
}
}

当我用以下函数调用此函数时:

<script type="text/javascript">document.write(getElementByClass('done'));</script>

什么都没发生。这个功能有问题吗?

6 个答案:

答案 0 :(得分:2)

document.getElementsByClassName('done');

编辑:

src:http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/

/*
    Developed by Robert Nyman, http://www.robertnyman.com
    Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
        getElementsByClassName = function (className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
                nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
                returnElements = [],
                current;
            for(var i=0, il=elements.length; i<il; i+=1){
                current = elements[i];
                if(!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = "",
                xhtmlNamespace = "http://www.w3.org/1999/xhtml",
                namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
                returnElements = [],
                elements,
                node;
            for(var j=0, jl=classes.length; j<jl; j+=1){
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = [],
                elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
                current,
                returnElements = [],
                match;
            for(var k=0, kl=classes.length; k<kl; k+=1){
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for(var l=0, ll=elements.length; l<ll; l+=1){
                current = elements[l];
                match = false;
                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};

答案 1 :(得分:2)

此函数没有获得具有该类名的所有元素,它获得一个。你打电话的方式是什么意思?对于您网页上已有的DOM元素,document.write似乎很有趣。

我讨厌只说“使用jquery”......但你可能应该这样做。

除了遗漏的ie声明之外,此功能确实有效。您将遇到的一个问题是,如果元素上有多个类,则此函数将无效。

答案 2 :(得分:1)

如果您已经发布完整的来源,那么是什么        var elements =(ie)? document.all:document.getElementsByTagName('*'); 尝试通过firebug跟踪错误。

答案 3 :(得分:1)

var elements = (ie)引用了未定义的变量ie

答案 4 :(得分:1)

您是否考虑过使用JavaScript库?像这样的函数写了许多times before,在这些事情上浪费时间是痛苦的。

答案 5 :(得分:0)

您应该在可用时使用getElementsByClassName。