简体   繁体   English

为什么不能只使用“ url(imageURL)”来设置背景图像?

[英]Why can't I just use “url(imageURL)” to set a background pictuer?

<img class = "preview" alt = "Styling with a Bandana" 
src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg"
 onmouseover = "upDate(this)" onmouseout = "unDo()">

function upDate(previewPic) 
{
 imageURL = previewPic.src;
 document.getElementById("image").style.backgroundImage =  "url('" + imageURL + "')";
}

Why should I use "url('" + imageURL + "')" to set background image? 为什么要使用"url('" + imageURL + "')"设置背景图像? I had tried to use "url(imageURL)" but it couldn't work. 我曾尝试使用"url(imageURL)"但无法使用。

ImageURL是一个variableurl函数需要一个字符串,因此将其放在引号之间是有意义的,以便它评估其内容(图片的路径)

您将ImageURL用作变量,因此在引号标记内将其变为字符串,因此需要将直接图像文件放入其中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM