英文:
I want to create javascript string so that when it is passed inside a div tag it shows as a list
问题
我正在尝试使用JavaScript在div
标签内以特定格式显示包含项目列表的字符串。该字符串包含多个项目,我希望将其显示为项目符号列表。以下是示例字符串:
const items = "这些是项目:\n1. 苹果\n2. 芒果";
我希望格式化字符串,使其在div
标签内显示如下:
这些是项目:
1. 苹果
2. 芒果
我正在使用React和Tailwind CSS,此问题涉及到一个React组件。
英文:
I am trying to display a string containing a list of items in a specific format inside a div
tag using JavaScript. The string contains several items that I want to display as a bulleted list. Here's the example string:
const items = "These are the items:\n1. apple\n2. mango";
I want to format the string so that it appears as follows inside the div
tag:
These are the items:
1. apple
2. mango
I am using React and Tailwind CSS, and this question pertains to a React component.
答案1
得分: 2
你可以迭代map方法,每当遇到任何'/n'时拆分它。此外,您还可以为此创建一个有序列表。例如,找到下面的代码。
import React from 'react';
const items = "\n1. apple\n2. mango";
const ListComponent = () => {
const itemList = items.split('\n').map((item, index) => {
const trimmedItem = item.replace(/^\d+\.\s/, '');
if (item.trim()) {
return <li key={index}>{ trimmedItem}</li>;
}
return null;
});
return (
<div>
<p>这是列表:</p>
<p>这些是项目:</p>
<ol>{itemList}</ol>
</div>
);
};
export default ListComponent;
这是上述代码运行时的截图:
英文:
You can iterate over the map method, by splitting it whenever any '/n' is encountered. Also, you can create an ordered list for this purpose as well. For instance, find the code below.
import React from 'react';
const items = "\n1. apple\n2. mango";
const ListComponent = () => {
const itemList = items.split('\n').map((item, index) => {
const trimmedItem = item.replace(/^\d+\.\s/, '');
if (item.trim()) {
return <li key={index}>{ trimmedItem}</li>;
}
return null;
});
return (
<div>
<p>Here is the list:</p>
<p>These are the items:</p>
<ol>{itemList}</ol>
</div>
);
};
export default ListComponent;
Here the screenshot for the above code when runs
running the above code
答案2
得分: 1
你可以将它传递给 innerText
:
const items = "这些是物品:\n1. 苹果\n2. 芒果";
document.getElementById("thediv").innerText = items;
<div id="thediv"></div>
英文:
You can pass it as innerText
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const items = "These are the items:\n1. apple\n2. mango";
document.getElementById("thediv").innerText=items;
<!-- language: lang-html -->
<div id="thediv"></div>
<!-- end snippet -->
答案3
得分: 1
// 你应该通过换行字符来拆分字符串,然后将其映射为多个段落标签
const items = "这些是物品:\n1. 苹果\n2. 芒果";
// 或者如果你想要它具有响应性:
const [items, setItems] = useState("这些是物品:\n1. 苹果\n2. 芒果");
然后在HTML中:
<div className="list">
{items.split('\n').map((el) => (
<p>{el}</p>
))}
</div>
现在列表正确显示,如果你想让物品居中并且希望它们左对齐,只需在列表的 CSS 类中添加 text-align: left; 即可。
英文:
You should split the string by the newline character, then map it into multiple paragraph tags
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");
Then in the html:
<div className="list">
{items.split('\n').map((el) => (
<p>{el}</p>
))}
</div>
Now the list is displayed correctly, if the items are centered and you want them to be aligned left just put text-align: left; in the list css class
答案4
得分: 0
const string = "这些是项目:\n1. 苹果\n2. 芒果";
console.log(string.split("\n"));
英文:
const string = "These are the items:\n1. apple\n2. mango";
console.log(string.split("\n"));
答案5
得分: 0
你应该解析你的字符串以获得一个列表。
一个非常简单的解析方法是:
items.split('/n')
然后你会得到
["这些是项目:", "1. 苹果", "2. 芒果"]
现在很容易显示。
英文:
You should parse your string in order to have a list.
A very simple parse would be :
items.split('/n')
and you would receive
["These are the items:", "1. apple", "2. mango"]
which is now easy to display
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论