提问者:小点点

从输入文件url将图像放在背景上


<script>
var loadFile = function(event) {
    var img=URL.createObjectURL(event.target.files[0]);
    document.getElementById('Display1').style.backgroundImage= url(img);
};
</script>

<div class="HM1EI" id="Display1">
 <button type="button" class="_1q_T1">
  <label>
   <input type="file"  accept="image/*" name="image" id="file1"  onchange="loadFile(event)" style="display: none;">
    Add Media
  </label>

</button>
</div>

我不能在后台上传我从输入类型文件得到的文件。


共1个答案

匿名用户

您没有正确设置后台url您应该这样做

document.getElementById('Display1').style.backgroundImage = `url('img_tree.png')`;

将(url)放入字符串中