简体   繁体   English

如何使用 JavaScript 从用户输入 (URL) 更改背景图像

[英]How to change background image from user input (URL) using JavaScript

How I'm supposed to change the HTML background image using the input user, the input is URL Link.我应该如何使用输入用户更改 HTML 背景图像,输入是 URL 链接。

    <input type="text" id="value">
    <button onclick="fun()">Change</button>

I tried something like this but it does not work.我试过这样的事情,但它不起作用。

function fun (){
      var value = document.getElementById("value").value;
      document.body.style.background = 'url(value)';
    }

Its because you are not using the value of "value".这是因为您没有使用“价值”的价值。 You need to concat the variable between the string.您需要在字符串之间连接变量。

Try this:尝试这个:

<input type="text" id="value">
<button onclick="fun()">Change</button>
function fun(){
      var value = document.getElementById("value").value;
      document.body.style.background = "url("+ value + ")";;
}

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

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