如何更改 HTML 元素样式,然后使用 JS 在 onLoad() 上恢复它?

发布于 2024-09-04 02:10:08 字数 205 浏览 4 评论 0原文

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

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

I have a problem with a Lightbox type modal window not working properly with IE. I have identified the problem as being due to the style attribute of the hidden image as 'display: none'. This works fine in all other browsers. But for IE, I need to change the value onload to 'display: block' and then straight back to 'display: none' which will fix the problem.

Could anyone tell me how I can do this? I need to apply this rule to multiple divs of class 'image'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-11 02:10:08

的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();
});

In your css

.image{ display:none; }

in your 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 version:

$(document).ready(function(){
    $(".image").show();
});
咿呀咿呀哟 2024-09-11 02:10:08

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

我不确定这是否适用于 IE,但可能会:

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

未能将其放入您的正文/文档 onLoad 函数中(如果您没有,请创建一个)。

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

编辑:修正我可怕的拼写。

are you using any frameworks at all? I ask because this can change the location that you put the following code.

I'm not positive if this works in IE but it may:

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

Failing that put it in your body/document onLoad function (if you don't have one create one).

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

Edit: fixing my horribad spelling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文