简体   繁体   English

无法更改div的背景图片

[英]Can't change background-image of a div

I'm trying to change background-image of a div #image by hovering on thumbnail images (something like a simple slider) My plunker is provided below. 我试图通过将鼠标悬停在缩略图上来更改div #image的背景图像(类似于简单的滑块),下面提供了我的插件。

1- First I tried to do like: element.style.backgroundImage but i got this in the console: 1-首先,我尝试这样做:element.style.backgroundImage但我在控制台中得到了这个: 使用.style.backgroundImage

2- Then i tried something like element.backgroundImage although it looks good in the console but didnt work, here is the console: 2-然后我尝试了诸如element.backgroundImage之类的东西,尽管它在控制台中看起来不错,但是没有用,这是控制台: 没有.style

Note: I don't want to use any library, just want to study JavaScript core. 注意:我不想使用任何库,只想学习JavaScript核心。

      my plunker

https://plnkr.co/edit/Mk0HSTYvQ4isVKjM7IEt?p=preview https://plnkr.co/edit/Mk0HSTYvQ4isVKjM7IEt?p=preview

update: 更新:

1- I noticed that i was doing wrong interpolation here: 1-我注意到我在这里做错了插值:

var bgI_value = 'url('+ src + ');';

it should be: 它应该是:

var bgI_value = 'url("'+ src + '");';

2- The suggestion from Alexandru to remove the simicolon was a good point. 2-亚历山德鲁的建议删除simicolon是一个好主意。

3- the suggestion from Sharma to add .style.backgroundImage was a good point too. 3- Sharma的建议添加.style.backgroundImage也是一个好主意。

so my code was missing these three corrections! 因此我的代码缺少这三个更正!

Thanks all. 谢谢大家

The issue is in the way you are changing the background image: 问题在于您更改背景图片的方式:

image.backgroundImage= bgI_value;

What it should be is: 应该是:

image.style.backgroundImage= bgI_value;

Note that this here is correct: 请注意,这是正确的:

var bgI_value = 'url('+ src + ')';

Here is the Plunker: https://plnkr.co/edit/NX1cgmzudJjFbM80q3eI?p=preview 这是Plunker: https ://plnkr.co/edit/NX1cgmzudJjFbM80q3eI p = preview

Here is your solution: 这是您的解决方案:

Please change 请更换

var bgI_value = 'url('+ src + ');';

TO

var bgI_value = 'url('+ src + ')';

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

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