为了账号安全,请及时绑定邮箱和手机立即绑定

将javascript元素背景图像设置为等于下载网址

将javascript元素背景图像设置为等于下载网址

动漫人物 2023-10-10 18:07:50
我正在尝试为 javascript 元素提供背景图像。此网址存储在数据库中,不是字符串,而是文件路径:https://firebasestorage.googleapis.com/v0/b/fir-firebase-182ca.appspot.com/o/images%2FScreenshot%20 (2 ).png?alt=media&token=0edf4c43-e791-48b1-b2f4-b8ec5e65e296我还制作了一个 java 脚本元素,它是一个 div,将其背景图像设置为此链接:var icon = document.createElement('img');icon.style.cssText = "background-color: red;"//icon.src = "'" + url + "'";var parent = document.getElementById("loggedDisplay");parent.appendChild(icon);到目前为止我还无法让它发挥作用。我怎样才能让这个背景图片= url
查看完整描述

2 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

尝试这个,

document.getElementById("Your div ID").style.backgroundImage = "url('https://firebasestorage.googleapis.com/v0/b/fir-firebase-182ca.appspot.com/o/images%2FScreenshot%20(2).png?alt=media&token=0edf4c43-e791-48b1-b2f4-b8ec5e65e296')";



查看完整回答
反对 回复 2023-10-10
?
慕村225694

TA贡献1880条经验 获得超4个赞

您也可以直接使用样式对象。但对于 css 背景,您需要将图像 url 放在“url('')”中


function myFunction() {

  var imagePath = "https://firebasestorage.googleapis.com/v0/b/fir-firebase-182ca.appspot.com/o/images%2FScreenshot%20(2).png?alt=media&token=0edf4c43-e791-48b1-b2f4-b8ec5e65e296";


  var icon = document.createElement('img');


  icon.style.backgroundColor = "red";


  //icon.src =  imagePath ;


  icon.style.backgroundImage = "url('" + imagePath + "')";


  var parent = document.getElementById("loggedDisplay");

  parent.appendChild(icon);


}

#loggedDisplay img {

  display: block;

  width: 100%;

  height: 300px;

}

<!DOCTYPE html>

<html>


<body>


  <button onclick="myFunction()">Try it</button>

  <div id="loggedDisplay"></div>


</body>


</html>


查看完整回答
反对 回复 2023-10-10
  • 2 回答
  • 0 关注
  • 36 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信