简体   繁体   English

如何从字符串正确显示项目符号?

[英]How can I display bullet points properly from a string?

So I am getting a string containing a bulleted list and its displaying like this:所以我得到一个包含项目符号列表的字符串,其显示如下:

blah * blah * blah呸*呸*呸

when I'd like it to display like:当我希望它显示为:

  • blah废话
  • blah废话
  • blah废话

my thinking is that I would break the string into an array, run through it in a for loop and splice in a new line right before the point.我的想法是我会将字符串分解成一个数组,在 for 循环中遍历它并在该点之前拼接新行。 then just return it .toString()然后就返回它 .toString()

var charArry = userInfo.split('');
for (var i = 1; i < charArry.length; i++) {
if (i == '\u2022') 
charArry.splice(i, 0, '\n');
}
return charArry.toString();

but maybe that's a terrible approach.但也许这是一个可怕的方法。 any ideas?有什么想法吗?

Instead of writing things out in text format, create li elements and append them to a ul element.不是以文本格式写出来,而是创建li元素并将它们附加到ul元素。

 let userInfo = 'blah1 blah2 blah3' var charArry = userInfo.split(' '); let ul = document.querySelector('#ul') charArry.forEach(liTxt => { let li = document.createElement('li') li.innerHTML = liTxt ul.appendChild(li) })
 <ul id='ul'></ul>

Split the string on "\•" and then use <ul> to display bullets.在 "\•" 上拆分字符串,然后使用<ul>显示项目符号。

let str = "blah•blah•blah";

let strArr = str.split("\u2022")

If you really want to add bullets to the string only then you may do something like this:如果您真的只想向字符串添加项目符号,则可以执行以下操作:

let newStr = '\u2022' + strArr.join("\n\u2022");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM