如何在javascript中证明文本

时间:2018-02-14 05:28:16

标签: javascript jquery html

我的数组中有很多字符串

const arr = ["Hello my name is jimmy", "will you go to the cinema with me?", "who's him?"]

我希望从数组中检索字符串并将其放在我的列表中,所以我先将它循环,但我想让文本对齐,因为我设置了列表的最大宽度{{1} }

eg: 100px

我从这段代码得到的结果是

<span style="width: 100px" id="hello">
</span>

arr.forEach(e => {
   const str1 = e.substr(0,10)
   const str2 = e.substr(10) 
   $('#hello').append(str1 + '\n' + str2 + '\n')
})

但我希望这样:

Hello my n
ame is jimmy
will you g
o to the cinema with me?
who's him?
(white space)

如何让它动态化? Hello my name is jimmy will you go to the cinema with me? who's him? (white space)

1 个答案:

答案 0 :(得分:2)

您可以尝试如下

&#13;
&#13;
var arr = ["Hello my name is jimmy", "will you go to the cinema with me?", "who's him?"]
arr.forEach(e => {
   $('#hello').append(e+" ")
})
&#13;
#hello{
  display:block;
  width:100px;
  text-align:justify;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span style="width: 100px" id="hello">
</span>
&#13;
&#13;
&#13;