使用javascript加载css隐藏图像作为页面加载时的背景图像
Noob在javascript上搜索,但没有运气。是否可以使用javascript加载html主体中已经存在的图像(css可见性隐藏),以在页面加载时作为div中的背景图像显示?是 就像使用javascript加载css隐藏图像作为页面加载时的背景图像,javascript,jquery,image,background-image,onload,Javascript,Jquery,Image,Background Image,Onload,Noob在javascript上搜索,但没有运气。是否可以使用javascript加载html主体中已经存在的图像(css可见性隐藏),以在页面加载时作为div中的背景图像显示?是 就像 <script type="text/javascript"> window.onload = function() { // get the image url from the src attribute of image imgurl =
<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>
window.onload=函数(){
//从图像的src属性获取图像url
imgurl=document.getElementById(“yourImgID”).src;
//将其设置为所需div的背景
document.getElementById(“yourDivID”).style.background=“url(“+imgurl+”)无重复”;
}
你可以看到一个提琴样本测试一下,你会发现的。谢谢你,伙计!我花了一天的时间去寻找,你只花了几分钟!