我正在尝试使用 JavaScript 在 div 标记内显示一个包含特定格式的项目列表的字符串。该字符串包含我想要显示为项目符号列表的几个项目。这是示例字符串:
const items = "These are the items:\n1. apple\n2. mango";
我想格式化字符串,使其在 div 标记内显示如下:
These are the items: 1. apple 2. mango
我正在使用 React 和 Tailwind CSS,这个问题与 React 组件有关。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您应该用换行符分割字符串,然后将其映射到多个段落标签
const items = "These are the items:\n1. apple\n2. mango"; // or if you want it t be reactive: const [items, setItems] = useState("These are the items:\n1. apple\n2. mango");然后在html中:
<div className="list"> {items.split('\n').map((el) => ( <p>{el}</p> ))} </div>现在列表可以正确显示,如果项目居中并且您希望它们左对齐,只需输入 text-align: left;在列表 css 类中