
在 react 中为 map 循环创建的 div 添加行号
为了给循环创建的 div 添加行号,可以采用以下方案:
首先,需要创建一个作为行号容器的 div,设置其 position 为 absolute,并放置在左侧。然后,将循环创建的 div 设置 position 为 relative,并设置父元素为 overflow: hidden。通过这种方式,可以将行号置于 div 的左侧。
具体实现代码如下:
// index.js
import react from "react";
import reactdom from "react-dom";
import "./styles.css";
function app() {
const itemdata = new array(500).fill(0);
const indexdata = new array(20).fill(0);
return (
{indexdata.map((item, index) => (
{index + 1}
))}
{itemdata.map((item, index) => (
))}
);
}
const rootelement = document.getelementbyid("root");
reactdom.render( , rootelement);// styles.css
.App {
font-family: sans-serif;
text-align: center;
overflow: hidden;
position: relative;
}
.left-box {
position: absolute;
left: 0;
top: 0;
width: 19px;
overflow: hidden;
}
.right-box {
margin-left: 19px;
}










