javascript - 使用javascript在页面加载时加载css隐藏图像作为背景图像

标签 javascript jquery image background-image onload

JavaScript 菜鸟,但搜索没有成功。是否可以使用javascript加载已经存在于html正文中的图像(css可见性隐藏),以便在页面加载时作为div中的背景图像可见?

最佳答案

是的。

喜欢就好

<script type="text/javascript">

      window.onload = function() {

          // get the image url from the src attribute of image
          imgurl =  document.getElementById("yourImgID").src;
          // set it as background to the div you want
          document.getElementById("yourDivID").style.background = "url("+imgurl+") no-repeat";

      }

</script>

您可以看到一个示例 fiddle here

关于javascript - 使用javascript在页面加载时加载css隐藏图像作为背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16730176/

相关文章:

javascript - 为什么这个选择表单在 Firefox 中不起作用?

javascript - jQuery Slider - 如何根据另一个 slider 制作一组具有最大值的 slider ?

javascript - jQuery.ajax 调用在 Chrome 扩展中失败

javascript - 通过 AJAX 调用接收到图像后立即显示动态生成的图像

javascript - 当 radio 改变时更新选择相同的值

javascript - 使用 Amazon Dynamodb 进行不区分大小写的查询

javascript - 非异步路径返回什么?

javascript - 根据 div 的宽度动态切换类

python - Django 管理员不显示来自 Imagefield 的图像

string - 图像稳定光流