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

使用 props 为 div 添加背景

使用 props 为 div 添加背景

繁花如伊 2021-09-30 10:16:10
我正在创建一个滑块并使用 prop 向它添加数据。每个幻灯片 div 都有一个标题和一个描述。我还想为每个 div 提供不同的图像背景。是否可以使用 props 向 div 添加背景图像?这是我的方法,但它不起作用:const slideData = [    {        id: 1,        heading: "First Heading",        description: "A short description of the service goes here",        image: './images/candle.jpg'    },    {        id: 2,        heading: "Second Heading",        description: "A short description of the service goes here",        image: './images/candle.jpg'    },    {        id: 3,        heading: "Third Heading",        description: "A short description of the service goes here",        image: './images/candle.jpg'    },    {        id: 4,        heading: "Fourth Heading",        description: "A short description of the service goes here",        image: './images/candle.jpg'    }]export default slideData和子组件import React from 'react';function Slide(props) {    return (        <div className="slide" style={{background: props.image}}>            <h3> { props.heading} </h3>            <p> { props.description} </p>        </div>    );}export default Slide;
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

也许这个代码片段会帮助你

style={{ backgroundImage: `url(${props.image})`


查看完整回答
反对 回复 2021-09-30
  • 2 回答
  • 0 关注
  • 232 浏览
慕课专栏
更多

添加回答

举报

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