简体   繁体   English

显示/隐藏JavaScript在IE中无效,仅在Firefox中有效

[英]Show/Hide JavaScript not working in IE, only in Firefox

This is the show/hide script I found online. 这是我在网上找到的显示/隐藏脚本。 When you click on the "view menu" image it's supposed to show the hidden div for each section. 当您单击“视图菜单”图像时,应该显示每个部分的隐藏div I'm not getting it to display/expand on Internet Explorer for some reason but it works fine in Firefox. 由于某种原因,我没有使其在Internet Explorer上显示/扩展,但在Firefox中运行良好。
http://www.abatchoflove.com http://www.abatchoflove.com

<script type="text/javascript">
    function unhide(divID) {
        var item = document.getElementById(divID);
        if (item) {
            item.className = (item.className == 'hidden') ?'unhidden':'hidden';
        }
    }
</script>

And this was the CSS portion. 这就是CSS部分。

<style type="text/css">
    .hidden { display: none; }
    .unhidden { display: block; }
</style>

This is what the code looks like in the sections. 这就是各节中的代码。

<td valign="top" bgcolor="#51BEB7">
    <a href="javascript:unhide('cookiesmenu');">
        <img src="images/cookies_nav.gif" border="0" usemap="#Map2">
    </a>
    <div id="cookiesmenu" class="hidden">
        <img src="images/cookies_menu.gif">
    </div>
</td>


Would love it if I could get a little help... Or if there is another way to go about it. 如果我能得到一点帮助,我会很乐意...或者还有其他解决方法。 Thank you in advance. 先感谢您。

you can use 您可以使用

<a onclick="unhide('cookiesmenu')">

instead of 代替

<a href="javascript:unhide('cookiesmenu');">

A Internet Explorer reserved word has been used as an identifier (function or variable name). Internet Explorer保留字已用作标识符(函数或变量名)。

change item by another variable name and give it a try. 通过另一个变量名更改项目并尝试一下。

http://www.ecma-international.org/ecma-262/5.1/#sec-7.6.1.1 http://www.ecma-international.org/ecma-262/5.1/#sec-7.6.1.1

http://www.springenwerk.com/2009/01/is-reserved-word-in-internet-explorer-6.html http://www.springenwerk.com/2009/01/is-reserved-word-in-internet-explorer-6.html

IE Issue with jquery and JS jQuery和JS的IE问题

If you want to test first if this is your issue, here is your script with a different variable name. 如果要首先测试这是否是问题所在,请使用不同的变量名来编写脚本。

function unhide(divID) {
    var MyItem = document.getElementById(divID);
    if (MyItem) {
        MyItem.className = (MyItem.className == 'hidden') ? 'unhidden' : 'hidden';
    }
}

http://jsfiddle.net/GvKLX/1/ http://jsfiddle.net/GvKLX/1/

Note that 'inlined javascript' is tending to disapear for security reasons. 请注意,出于安全原因,“内联javascript”趋向于消失。 So you should consider reforming your code using javascript libraries such has jQuery. 因此,您应该考虑使用具有jQuery的javascript库来重新编写代码。 Meaning, even javascript events like 'onclick' are to become obsolete. 这意味着,甚至像“ onclick”之类的javascript事件也将过时。

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

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