根据提示更改背景 url(img)

Change background url(img) based on prompt

我有:

var img = prompt("Enter your background url here");

然后我添加:

var img = prompt("Enter your background url here");
document.body.style.backgroundImage = "url(img)"; //img is a var

但是没用!

尝试使用 template literals:

var img = prompt("Enter your background url here");
document.body.style.backgroundImage = `url(${img})`;

因为img是一个变量,所以需要用url

拼接

var img = prompt("Enter your background url here");
document.body.style.backgroundImage = "url(" + img + ")";

你能试试吗

document.body.style.backgroundImage = "url("+image+")";

您需要正确连接 img

var img = prompt("Enter your background url here");
document.body.style.backgroundImage = "url('"+img+"')";