代码之家  ›  专栏  ›  技术社区  ›  igniteflow

如何更改HTML元素样式,然后使用JS将其还原为onload()?

  •  1
  • igniteflow  · 技术社区  · 14 年前

    我有一个问题是,lightbox类型的模式窗口不能与IE一起正常工作。我已经确定该问题是由于隐藏图像的样式属性“display:none”造成的。这在所有其他浏览器中都可以正常工作。但对于IE,我需要将值onload更改为“display:block”,然后直接返回到“display:none”,这将解决问题。

    有人能告诉我怎么做吗?我需要将此规则应用于类“image”的多个div。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Gregoire    14 年前

    在你的CSS中

    .image{ display:none; }
    

    在HTML中

    ...
    <body onload="switch" >
    ...
    
    
    <script type="text/javascript">
    function switch(){
        var divs = document.getElementsByClassName("image"); // not supported by all browsers but you can easily find implementations on the web
    
        for(var index=0;index<divs.length;index++)
        {
            divs[index].style.display='block';
        }
    }
    </script>
    
    </body>
    

    jQuery版本:

    $(document).ready(function(){
        $(".image").show();
    });
    
        2
  •  0
  •   Matt S    14 年前

    你在使用任何框架吗?我这样问是因为这可以更改您放置以下代码的位置。

    如果这在IE中有效,我不确定,但它可能:

    <img ... onload="this.style.display='block';this.style.display='none';">
    

    如果失败,请将其放入body/document onload函数中(如果您没有创建一个)。

    function docOnLoad()
    {
       ...
       document.getElementById('img_in_question').style.display = 'block';
       document.getElementById('img_in_question').style.display = 'none';
       ...
    }
    

    编辑:修正我糟糕的拼写。