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

标签 javascript css image

我目前正在尝试将 JS 变量的属性从 url("../images/video.png")(它在 CSS 中的声明方式)更改为 url("../images/pause. png") 通过这行代码:

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

将鼠标悬停在图像上会显示图像,并且两个图像都在文件夹中。但是,一旦执行了这行代码,最初显示的图像(video.png)就会变成灰色。我在控制台中遇到的错误是“Uncaught SyntaxError: Unexpected token”。在该代码所在的行上。在此先感谢您的帮助!

最佳答案

您在一行中使用了两个双引号。您必须转义它们或使用单引号:

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

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

两者都应该有效。

关于javascript - 如何在 JS 中更改背景图片属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38213138/

相关文章:

html - 响应式设计 : background image button

asp.net - ASP :GridView GridLines ="None" 的样式

html - “粘贴”图像到页面右侧

image - 连接二值图像中的不相交边

javascript - 使用 JQuery 定位数组中的元素(语法)

javascript - json 值中的单引号

javascript - 从数组加载 HTML5 视频播放器

javascript - 如何使用阅读更多切换动态文本

html - 如何相对于另一个图像调整图像的大小?

javascript - 使背景在用户输入时消失。 CSS