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/