简体   繁体   English

如何在JS中更改背景图片属性

[英]How to Change Background Image Attribute in JS

I'm currently attempting to change an attribute of a JS variable from url("../images/video.png") (how it was declared in the CSS) to url("../images/pause.png") via this line of code: 我目前正在尝试将JS变量的属性从url(“ ../ images / video.png”)(在CSS中如何声明)更改为url(“ ../ images / pause.png”)通过这一行代码:

fullscreenPlayPauseButton.style.backgroundImage = "url("../images/pause.png")"; fullscreenPlayPauseButton.style.backgroundImage =“ url(” ../ images / pause.png“)”“;

The mouse over of the image is showing the image and both images are in the folder. 图像上的鼠标悬停显示该图像,并且两个图像都在文件夹中。 However, once this line of code is executed, the image that originally showed up (the video.png) turns grey. 但是,一旦执行了这一行代码,最初显示的图像(video.png)就会变成灰色。 The error I'm getting in console is "Uncaught SyntaxError: Unexpected token ." 我在控制台中遇到的错误是“ Uncaught SyntaxError:意外的令牌”。 on the line where that code is. 在该代码所在的行上。 Thanks in advance for the help! 先谢谢您的帮助!

You are using two doublequotes in one line. 您在一行中使用两个双引号。 You have to escape them or use single quotes: 您必须转义它们或使用单引号:

fullscreenPlayPauseButton.style.backgroundImage = "url('../images/pause.png')";

or 要么

fullscreenPlayPauseButton.style.backgroundImage = "url(\"../images/pause.png\")";

Both should work. 两者都应该起作用。

尝试这样设置属性:

fullscreenPlayPauseButton.style.backgroundImage = "url('../images/pause.png')";

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

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