简体   繁体   English

this.id不工作在ie

[英]this.id not working in ie

I've been putting together an accordion-style left-navigation using JavaScript, and I've run into a problem where the following code works in Chrome and Firefox but not in IE. 我一直在使用JavaScript组合手风琴式的左导航,我遇到了一个问题,下面的代码在Chrome和Firefox中运行,但在IE中却不行。

function navClick(clickedDD){
    var numDD = document.getElementsByClassName('navDropdown');
    var numArrow = document.getElementsByClassName('navUnfolded');
    var selectedDD = document.getElementById('dd0' + clickedDD);
    var arrow = document.getElementById('arrow0' + clickedDD);
    if (selectedDD.style.display == 'none') {
        for (var i = numDD.length - 1; i >= 0; i--) {
            numDD[i].style.display = 'none';
        }
    }
    if (arrow.className == 'navFolded') {
        for (var i = numArrow.length - 1; i >= 0; i--) {
            numArrow[i].className = 'navFolded';
        }
    }
    if (selectedDD.style.display == 'none') {
        selectedDD.style.display = 'block';
        arrow.className = 'navUnfolded';
    } else {
        selectedDD.style.display = 'none';
        arrow.className = 'navFolded';
    }
}

I'm not positive what's keeping it from functioning, but I suspect it's the "this.id" I'm using in the onclick. 我不肯定是什么让它无法正常运行,但我怀疑这是我在onclick中使用的“this.id”。

<div id="1" onclick="navClick(this.id);">Content</div>

I'm relatively new to JavaScript, if that's helpful to know. 我对JavaScript比较陌生,如果这有助于了解。 Basically, this function first looks to see if the clicked div is displayed or not, and if not then it sets display to none for all the divs before continuing. 基本上,此函数首先查看是否显示单击的div,如果没有,则在继续之前将所有div的显示设置为none。 If it is displayed, it does nothing because the next bit will handle it. 如果显示,它什么都不做,因为下一位将处理它。 There's a similar rule right after for divs with an arrow image that changes. 对于具有变化的箭头图像的div,之后有类似的规则。 Then if it the div is not displayed, it sets it to 'block' or else it sets it to 'none', which allows me to open and close a single menu div without opening/closing the others. 然后,如果没有显示div,则将其设置为“阻止”,否则将其设置为“无”,这允许我打开和关闭单个菜单div而无需打开/关闭其他菜单。

I'm using the id collected by the onclick function to control a div with a different id, as can be seen in the bottom to variables at the top of the function. 我正在使用onclick函数收集的id来控制具有不同id的div,可以在函数顶部的变量底部看到。 Perhaps this is where IE is getting touchy. 也许这就是IE变得敏感的地方。

Any help is appreciated. 任何帮助表示赞赏。

On your element, try puttin the ID in quotes: 在您的元素上,尝试将ID放在引号中:

<div id = '1' onClick="navClick(this.id);">

You could also try using javascript's 'Alert' function to find out what the data looks like in IE: 您还可以尝试使用javascript的“警报”功能来查找IE中的数据:

alert(selectedDD); 

getElementsByClassName isn't supported in IE8 or below. IE8或更低版本不支持 getElementsByClassName

You could try Jonathan Snook's approach for IE8 and below compatibility if you don't want to use a full library. 如果您不想使用完整的库,可以尝试使用Jonathan Snook的 IE8及以下兼容性方法。

Your id in <div id="1" .. is not a valid one: 您在<div id="1" ..不是有效的:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID和NAME令牌必须以字母([A-Za-z])开头,后面可以跟任意数量的字母,数字([0-9]),连字符(“ - ”),下划线(“_”) ,冒号(“:”)和句号(“。”)。

so try changing it for valid one (eg. #a1 ) as that may be the cause of your problem. 所以尝试将其更改为有效的(例如#a1 ),因为这可能是您的问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM