javascript - 我正在尝试在 jsx-react 中动态添加图像,但它不会显示
问题描述
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”分配了一个值,但未使用它。
解决方案
输出:
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
);
推荐阅读
- ethernet - Xilinx Echo 服务器数据变量
- reactjs - React useReducer 钩子和功能组件生命周期保证
- apache-spark - Prometheus 中的 Spark 3.0 流式指标
- python - 创建仅包含原始列表中唯一值的新列表
- html - 线性渐变不覆盖整个图像(留下〜1px的边界)
- c - 在 C 中如何将新节点插入到链表中?
- git - 我以前的更改仍然计入新 PR
- azure - 如何创建免费 Azure 订阅承诺的免费 250GB SQL Server 数据库
- vue.js - Vue3 安装时出现 _FSEventStreamCreate 错误
- python - 哪个 Selenium 方法或命令可用于单击以下按钮?