English 中文(简体)
贾瓦文
原标题:JavaScript get child element

为什么这不会在火ox中发挥作用,试图选择这一类别,然后使子类明显可见。

<script type="text/javascript">
    function show_sub(cat) {
      var cat = document.getElementById("cat");
      var sub = cat.getElementsByName("sub");
      sub[0].style.display= inline ; 
}

</script>

——

<ul>
    <li id="cat" onclick="show_sub(this)">
        Top 1
        <ul style="display:none" name="sub">
            <li>Sub 1</li>
            <li>Sub 2</li>
            <li>Sub 3</li>
        </ul>
    </li>
    <li>Top 2</li>
    <li>Top 3</li>
    <li>Top 4</li>
</ul>

EDIT 答复是:

<script type="text/javascript">
   function show_sub(cat) {
      cat.getElementsByTagName("ul")[0].style.display = (cat.getElementsByTagName("ul")[0].style.display == "none") ? "inline" : "none";
   }
</script>
最佳回答

ULs没有名字属性,但你可以用标签提及该图。

用以下文字取代第3行:

var sub = cat.getElementsByTagName("UL");
问题回答

我建议采取类似行动:

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

虽然以上是使用<条码><>>。 而不是<代码> 姓名/代码>属性等于<代码>/>。

至于你最初的“干 t”版本(但我必须补充一点,对问题作特别有用的描述)的原因,我可以建议,在 Ch, Java文 con报告说:

攻击类型 错误:目标号没有方法获得要素 ByName 。

围绕老年和半衰期家庭进行工作的一种做法是使用一种习俗功能来复制getElementsByClassName(,尽管很粗略:

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

引证:

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);
};​

E6 &,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;
    }
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签