I want to create javascript string so that when it is passed inside a div tag it shows as a list

huangapple go评论70阅读模式
英文:

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 &#39;react&#39;;
    
const items = &quot;\n1. apple\n2. mango&quot;;

const ListComponent = () =&gt; {
    const itemList = items.split(&#39;\n&#39;).map((item, index) =&gt; {
        const trimmedItem = item.replace(/^\d+\.\s/, &#39;&#39;);
        if (item.trim()) {
          return &lt;li key={index}&gt;{ trimmedItem}&lt;/li&gt;;
        }
        return null;
    });

    return (
        &lt;div&gt;
            &lt;p&gt;Here is the list:&lt;/p&gt;
            &lt;p&gt;These are the items:&lt;/p&gt;
            &lt;ol&gt;{itemList}&lt;/ol&gt;
        &lt;/div&gt;
    );
};

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 = &quot;These are the items:\n1. apple\n2. mango&quot;;
document.getElementById(&quot;thediv&quot;).innerText=items;

<!-- language: lang-html -->

&lt;div id=&quot;thediv&quot;&gt;&lt;/div&gt;

<!-- 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 = &quot;These are the items:\n1. apple\n2. mango&quot;;

// or if you want it t be reactive:
const [items, setItems] = useState(&quot;These are the items:\n1. apple\n2. mango&quot;);

Then in the html:

    &lt;div className=&quot;list&quot;&gt;
      {items.split(&#39;\n&#39;).map((el) =&gt; (
         &lt;p&gt;{el}&lt;/p&gt;
       ))}
    &lt;/div&gt;

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 = &quot;These are the items:\n1. apple\n2. mango&quot;;
console.log(string.split(&quot;\n&quot;));

答案5

得分: 0

你应该解析你的字符串以获得一个列表。
一个非常简单的解析方法是:
items.split(&#39;/n&#39;)

然后你会得到

[&quot;这些是项目:&quot;, &quot;1. 苹果&quot;, &quot;2. 芒果&quot;]
现在很容易显示。

英文:

You should parse your string in order to have a list.
A very simple parse would be :
items.split(&#39;/n&#39;)

and you would receive

[&quot;These are the items:&quot;, &quot;1. apple&quot;, &quot;2. mango&quot;]
which is now easy to display

huangapple
  • 本文由 发表于 2023年6月16日 14:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定