javascript - 隐藏 html 元素在不同浏览器中的不同行为

标签 javascript html css google-chrome firefox

我有一个 div ,其中 display:none 作为 style 属性值。在css中,为这个div设置了一个背景图片url。我只是不想在稍后通过一些 JS 代码看到 div 之前触发对图像的请求。在 Firefox 中,网络选项卡显示请求未按预期发出。但在 Chrome 开发者工具中,我发现对图像的请求实际上是在 DOMContentLoaded 事件之后触发的。在这两种不同的浏览器中隐藏元素的不同行为的可能原因是什么?

标记:

<html>
<head>
    <title></title>
    <style type="text/css">
        .remoteAudioSoundButton{
            background: url("http://ourserverurl/images/image_lady.png");
            width: 200px;
            height: 200px;
            border: 2px black;
        }
    </style>
</head>
<body >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class='remoteAudioSoundButton' style="display:none"></div>
<script type="text/javascript">
    window.onload = function(){
        console.log("inside onload");
    };
</script>
</body>
</html>

屏幕截图:

Chrome:

enter image description here

火狐浏览器:

enter image description here

最佳答案

为什么不将背景添加到特定类别?这样,仅当特定类添加到元素时才会加载图像。

$(function(){
  $('button').click(function() {
    $('.remoteAudioSoundButton').toggleClass('visible');
  });
});
.remoteAudioSoundButton{
  display: none;
  width: 200px;
  height: 200px;
  border: 2px black;
}
.visible {
  background: url("http://ourserverurl/images/image_lady.png");
  display: block;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class='remoteAudioSoundButton'></div>
<button>Toggle Class</button>

关于javascript - 隐藏 html 元素在不同浏览器中的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689873/

相关文章:

javascript - Webpack 2.3.3 - 类型错误 : $export is not a function

javascript - 在图像悬停Jquery上显示div

javascript - 如何使用 jQuery 访问存储在数组中的元素?

php - CSS 通配符重写

php - 如何使用查询结果来确定 DIV 的类?

html - 选项卡自定义文件上传按钮

javascript - 在 JavaScript 中自动将数字范围转换为数字的 PHP 字符串

javascript - JQuery:如何使用 jQuery 检测移动宽度并将我的 css 更改为它?

html - CSS 样式表第一次没有链接到 HTML web-dev

javascript - Bootstrap 下拉菜单不起作用