简体   繁体   English

如何更改 js 文件中的背景图像而不是硬编码 url

[英]How to change background image in js file but not hard coding url

I cannot figure out why I can't change background images when I use onmouseover events.我无法弄清楚为什么我在使用onmouseover事件时无法更改背景图像。 My goal is when I put mouse over first pic, then this pic will show up on the top of page, when I move my mouse to another pic, then this new pic will show up on the top instead, then when I move mouse to the third pic, the third pic will show up on the top.我的目标是当我将鼠标放在第一张照片上时,这张照片会显示在页面顶部,当我将鼠标移动到另一张照片时,这张新照片将显示在顶部,然后当我将鼠标移动到第三张图片,第三张图片将显示在顶部。 Here I attached my html code and js code.这里附上我的 html 代码和 js 代码。 Please help me.请帮我。

 function upDate(previewPic){ // document.getElementById("image").style.backgroundColor = "green"; document.getElementById("image").style.backgroundColor = "url(previewPic.src)"; document.getElementById("image").innerHTML = previewPic.alt + ". this is onmouseover events"; } function unDo(){ document.getElementById("image").style.backgroundColor = "#8e68ff"; document.getElementById("image").innerHTML = "Hover over an image below to display here."; }
 <div id = "image"> Hover over an image below to display here. </div> <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()"> <img class = "preview" alt = "With My Boy" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG" onmouseover = "upDate(this)" onmouseout = "unDo()"> <img class = "preview" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg" alt = "Young Puppy" onmouseover = "upDate(this)" onmouseout = "unDo()">

'previewPic.src' seems to be a variable that has been used as a string. 'previewPic.src' 似乎是一个已用作字符串的变量。 You should replace this piece of code你应该替换这段代码

document.getElementById("image").style.backgroundColor = "url(previewPic.src)";

with

document.getElementById("image").style. backgroundImage = "url('"+previewPic.src+"')";
document.getElementById("image").style.backgroundColor = "#8e68ff";

Above code is not valid first of all, you need to provide a unique id to your HTML element like -上面的代码首先是无效的,你需要为你的 HTML 元素提供一个唯一的 id,比如 -

<img class = "preview"
 id="preview1"
 src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg"
 alt = "Young Puppy"
 onmouseover = "upDate(this)"
 onmouseout = "unDo()">

Now you can access this HTML image element using the id and change its src like this -现在您可以使用 id 访问此 HTML 图像元素并像这样更改其 src -

document.getElementById("preview1").src = "hackanm.gif";

If you are using multiple image elements, please note that you have to use different ids for them.如果您使用多个图像元素,请注意您必须为它们使用不同的 id。

You need to change below things.您需要更改以下内容。

  1. div should have backgroundImage not backgroundColor . div应该有backgroundImage而不是backgroundColor
  2. you have to set url this way "url('" + previewPic.src + "')"你必须这样设置url "url('" + previewPic.src + "')"
  3. Remove backgroundImage in unDo() with document.getElementById("image").style.backgroundImage = "";使用document.getElementById("image").style.backgroundImage = "";unDo()中删除backgroundImage

All you need to update is one line like this document.getElementById("image").style.backgroundImage = "url('" + previewPic.src + "')";您需要更新的只是一行这样的document.getElementById("image").style.backgroundImage = "url('" + previewPic.src + "')";

Check your result below.在下面检查您的结果。

 function upDate(previewPic){ // document.getElementById("image").style.backgroundColor = "green"; document.getElementById("image").style.backgroundImage = "url('" + previewPic.src + "')"; document.getElementById("image").innerHTML = previewPic.alt + ". this is onmouseover events"; } function unDo(){ document.getElementById("image").style.backgroundImage = ""; document.getElementById("image").style.backgroundColor = "#8e68ff"; document.getElementById("image").innerHTML = "Hover over an image below to display here."; }
 <div id = "image"> Hover over an image below to display here. </div> <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()"> <img class = "preview" alt = "With My Boy" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon2.JPG" onmouseover = "upDate(this)" onmouseout = "unDo()"> <img class = "preview" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon3.jpg" alt = "Young Puppy" onmouseover = "upDate(this)" onmouseout = "unDo()">

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

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