JavaScript get child element

后端 未结 3 1938
梦谈多话
梦谈多话 2020-12-13 08:56

Why this does not work in firefox i try to select the category and then make subcategory visible.



        
相关标签:
3条回答
  • 2020-12-13 09:36

    ULs don't have a name attribute, but you can reference the ul by tag name.

    Try replacing line 3 in your script with this:

    var sub = cat.getElementsByTagName("UL");
    
    0 讨论(0)
  • 2020-12-13 09:51

    I'd suggest doing something similar to:

    function show_sub(cat) {
        if (!cat) {
            return false;
        }
        else if (document.getElementById(cat)) {
            var parent = document.getElementById(cat),
                sub = parent.getElementsByClassName('sub');
            if (sub[0].style.display == 'inline'){
                sub[0].style.display = 'none';
            }
            else {
                sub[0].style.display = 'inline';
            }
        }
    }
    
    document.getElementById('cat').onclick = function(){
        show_sub(this.id);
    };​​​​
    

    JS Fiddle demo.

    Though the above relies on the use of a class rather than a name attribute equal to sub.

    As to why your original version "didn't work" (not, I must add, a particularly useful description of the problem), all I can suggest is that, in Chromium, the JavaScript console reported that:

    Uncaught TypeError: Object # has no method 'getElementsByName'.

    One approach to working around the older-IE family's limitations is to use a custom function to emulate getElementsByClassName(), albeit crudely:

    function eBCN(elem,classN){
        if (!elem || !classN){
            return false;
        }
        else {
            var children = elem.childNodes;
            for (var i=0,len=children.length;i<len;i++){
                if (children[i].nodeType == 1
                    &&
                    children[i].className == classN){
                        var sub = children[i];
                }
            }
            return sub;
        }
    }
    
    function show_sub(cat) {
        if (!cat) {
            return false;
        }
        else if (document.getElementById(cat)) {
            var parent = document.getElementById(cat),
                sub = eBCN(parent,'sub');
            if (sub.style.display == 'inline'){
                sub.style.display = 'none';
            }
            else {
                sub.style.display = 'inline';
            }
        }
    }
    
    var D = document,
        listElems = D.getElementsByTagName('li');
    for (var i=0,len=listElems.length;i<len;i++){
        listElems[i].onclick = function(){
            show_sub(this.id);
        };
    }​
    

    JS Fiddle demo.

    0 讨论(0)
  • 2020-12-13 09:52

    Try this one:

    function show_sub(cat) {
        var parent = cat,
        sub = parent.getElementsByClassName('sub');
        if (sub[0].style.display == 'inline'){
            sub[0].style.display = 'none';
        }
        else {
            sub[0].style.display = 'inline';
        }
    }
    
    document.getElementById('cat').onclick = function(){
        show_sub(this);
    };​
    

    and use this for IE6 & 7

    if (typeof document.getElementsByClassName!='function') {
        document.getElementsByClassName = function() {
            var elms = document.getElementsByTagName('*');
            var ei = new Array();
            for (i=0;i<elms.length;i++) {
                if (elms[i].getAttribute('class')) {
                   ecl = elms[i].getAttribute('class').split(' ');
                    for (j=0;j<ecl.length;j++) {
                        if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                            ei.push(elms[i]);
                        }
                    }
                } else if (elms[i].className) {
                    ecl = elms[i].className.split(' ');
                    for (j=0;j<ecl.length;j++) {
                        if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {
                            ei.push(elms[i]);
                        }
                    }
                }
            }
            return ei;
        }
    }
    
    0 讨论(0)
提交回复
热议问题