javascript - Why am I not able to show or hide in Internet Explorer 8, and how I can fix the problem? - Stack Overflow

I have written JavaScript code to show and hide the div. But I got stuck when it is not working in Inte

I have written JavaScript code to show and hide the div. But I got stuck when it is not working in Internet Explorer 8. It is working smoothly in other browsers, like Opera, Firefox etc.

Here is my code:

<html>
    <head>
        <title>Javascript Show Hide Div Visibility</title>

        <style type="text/css">
        </style>

        <script language="javascript" type="text/javascript">
        function showHideDiv()
        {
            var divstyle = new String();
            divstyle = document.getElementById("div1").style.visibility;
            if(divstyle.toLowerCase()=="visible" || divstyle == "")
            {
                document.getElementById("div1").style.visibility = "hidden";
            }
            else
            {
                document.getElementById("div1").style.visibility = "visible";
            }
        }
        </script>
    </head>

    <body>
        <div id="div1" class="divStyle">
            <object width="300" height="300">
                <param name="movie" value="">
                </param>
                <embed src=""
                       type="application/x-shockwave-flash"
                       width="300"
                       height="300">
                </embed>
            </object>
        </div>

        <center>
             <div onclick="showHideDiv()">Click Me For show hide <div>
        </center>
    </body>
</html>

I have written JavaScript code to show and hide the div. But I got stuck when it is not working in Internet Explorer 8. It is working smoothly in other browsers, like Opera, Firefox etc.

Here is my code:

<html>
    <head>
        <title>Javascript Show Hide Div Visibility</title>

        <style type="text/css">
        </style>

        <script language="javascript" type="text/javascript">
        function showHideDiv()
        {
            var divstyle = new String();
            divstyle = document.getElementById("div1").style.visibility;
            if(divstyle.toLowerCase()=="visible" || divstyle == "")
            {
                document.getElementById("div1").style.visibility = "hidden";
            }
            else
            {
                document.getElementById("div1").style.visibility = "visible";
            }
        }
        </script>
    </head>

    <body>
        <div id="div1" class="divStyle">
            <object width="300" height="300">
                <param name="movie" value="http://www.youtube./v/7_6B6vwE83U">
                </param>
                <embed src="http://www.youtube./v/7_6B6vwE83U"
                       type="application/x-shockwave-flash"
                       width="300"
                       height="300">
                </embed>
            </object>
        </div>

        <center>
             <div onclick="showHideDiv()">Click Me For show hide <div>
        </center>
    </body>
</html>
Share Improve this question edited Aug 24, 2011 at 18:31 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jun 7, 2011 at 9:21 umesh shrivasumesh shrivas 111 gold badge1 silver badge1 bronze badge 7
  • 1 You could just $("#div1").toggle() with jQuery – Flash Commented Jun 7, 2011 at 9:24
  • 2 Or not... jQuery is not needed in this case – mplungjan Commented Jun 7, 2011 at 9:29
  • 1 Your code looks fine. I tried your code, it's working in IE 8. Clear IE8 Browser cache and try again. – Muthu Kumaran Commented Jun 7, 2011 at 9:32
  • Don't forget to declare a doctype - IE8 is a bit sensitive... w3/QA/2002/04/valid-dtd-list.html – WSkid Commented Jun 7, 2011 at 9:34
  • @mplungjan jQuery is never needed but it makes things like this easier especially when you're worried about browser inconsistencies :) – Flash Commented Jun 7, 2011 at 9:38
 |  Show 2 more ments

6 Answers 6

Reset to default 1

try:

var div1 = document.getElementById("div1");
if(div1.style.display=="none" || div1.style.display == ""){
  document.getElementById("div1").style.diplay = "block";
}
else{
  div1.style.display = "none";
}

Try using display:

function showHideDiv()
{
    var divstyle = new String();
    divstyle = document.getElementById("div1").style.display;
    if(divstyle.toLowerCase()=="block" || divstyle == "")
    {
        document.getElementById("div1").style.display= "none";
    }
    else
    {
        document.getElementById("div1").style.display= "block";
    }
}

jQuery is your best friend when working with DOM.

<style type="text/css">
  .hidden {
    visibility: hidden;
  }
</style>

<script type="text/javascript">
    $(function(){
        $('.trigger').click(function(){
            $('#div1').toggleClass('hidden');
        });
    });
</script>

<div id="div1" class="divStyle">
    <object width="300" height="300">
        <param name="movie" value="http://www.youtube./v/7_6B6vwE83U"></param>
        <embed src="http://www.youtube./v/7_6B6vwE83U" type="application/x-shockwave-flash" width="300" height="300"></embed>
    </object> 
</div>

<center>
    <div class="trigger">Click Me For show hide</div>
</center>

Your page works just fine in Firefox 4 and Internet Explorer 8 on Windows XP.

Yours: http://jsfiddle/mplungjan/2KZ47/

Mine: http://jsfiddle/mplungjan/7bxrB/

<html>
    <head>
        <title>Javascript Show Hide Div Visibility</title>

        <style type="text/css">
            .center {text-align:center}
            #div1 {visibility:visible}
        </style>

        <script language="javascript" type="text/javascript">
            function showHideDiv() {
               var div = document.getElementById("div1");
               div.style.visibility=(div.style.visibility==="visible"||div.style.visibility==="")?"hidden":"visible";
            }
        </script>
    </head>

    <body>
        <div id="div1" class="divStyle">
        ...
        </div>

        <div class="center" onclick="showHideDiv()">Click Me For show hide <div>
    </body>
</html>

The respondents that have tested it all report that it works fine in their IE8. That would leave three options open:

  1. No Doctype declaration (I'm assuming the testers put that over it manually).
  2. It concerned an intranet site, temporarily or permanently. IE8+ has a Compatibility Mode/View (= IE7), which it defaults to in case an intranet site is requested. Exceptions are made for sites of which the URL starts with 'localhost' of '127.0.0.1', the address of the on-puter server(-simulator), if installed.
  3. The OP had his IE8 set to 'View all sites in Compatibility Mode/View'.

There is a filter property (style.filter) that IE8 sets it to alpha(opacity=0). Just set it to undefined and the element pops up again.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745133616a4613094.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信