javascript - 我试图在 jsx-react 中动态添加图像,但它不会显示

标签 javascript css reactjs jsx

import React from "react";
import ReactDOM from "react-dom";

var bImg = prompt("Which image do you want to set as background image?");
const bStyle = {
  backgroundImage: "url(bImg)" // i stored the link here 
};

const rootElement = document.getElementById("root");
ReactDOM.render(
  <div style={bStyle}> // I stored it here but still it won't show up
    <h1>Hello world!!</h1>
  </div>,
  rootElement
);

其余部分工作正常,但背景图像不会从我在提示中输入的 URL 加载。该错误表明“bImg”已赋值,但未使用。

最佳答案

输出:

enter image description here

import React from "react";
import ReactDOM from "react-dom";

import App from "./App";

ReactDOM.render(<App />, document.getElementById("root"));

var bImg = prompt("Which image do you want to set as background image?");
const bStyle = {
  backgroundImage: `url(${bImg})` // i stored the link here
};

const rootElement = document.getElementById("root");
ReactDOM.render(
  <div style={bStyle}>
    // I stored it here but still it won't show up
    <h1>Hello world!!</h1>
  </div>,
  rootElement
);

StackBlitz Demo

关于javascript - 我试图在 jsx-react 中动态添加图像,但它不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65103820/

相关文章:

MySQL Select 的 Javascript 值

javascript - 如何在jquery中通过迭代获取值来创建JSON对象

HTML/CSS 如何在不使用 "position:absolute;"的情况下创建覆盖某个容器的覆盖层?

reactjs - Material 界面 :Flickering on the initial load of the page(Razzle)

javascript - 将输入绑定(bind) react 到状态与局部变量

javascript - window.onload Javascript,具有接收参数的函数?

php - IE 未在 HTML 页面上正确应用 css/脚本(适用于 .php)

css - IE9 无法在 iFrame 中呈现 css 的原因有哪些?

reactjs - ESLint 详尽-deps : ignore value that came from a ref

javascript - 在 ckeditor 中切换实时预览