javascript - 如果文本太长,如何用空格分隔文本?

标签 javascript css

所以我觉得我有点进退两难。

我有一个具有固定宽度列但没有固定宽度行的网格。

所以,我有一个像这样的场景

Column1, row 1: 
ThisisalongtextstringthatdoesnotcontainspacesthatIneedtobreakup

Column2, row2:
This is a long text string that has whitespace and I would like to cut based on whitespace between words.

这是我尝试让这些字符串在我的网格中看起来不错:

.k-grid td {
    border-style: solid;
    border-width: 0px 0px 0px 1px;
    padding: 0.4em 0.6em;
    vertical-align: middle;
    white-space: normal;
    word-break: keep-all;
}

上述解决方案使用空格分解第二个字符串并将其限制为列宽,这使得字符串看起来可读。

但是上面的代码片段也没有分解上面的长字符串。

我使用了 using word-break:break-all; 这也很棒(因为第一个字符串被分解并且不会进入其他列)但是对于第二个字符串,它分手就像下面这样的词(我不想要):

Break-all 对空白单词执行此操作:

This is a long first name wit
h

这样 h 就挂掉了,但我更喜欢在第二行有 with 。

所以,我想知道如何解决这两个问题?

最佳答案

使用自动换行:break-word;:

From MDN's docs

break-word
Indicates that normally unbreakable words may be broken at arbitrary points if there are no otherwise acceptable break points in the line.

div {
  width: 100px;
  padding: 10px;
  outline: 1px solid silver;
  word-wrap: break-word;
}
<div>
  ThisisalongtextstringthatdoesnotcontainspacesthatIneedtobreakup
</div>
<div>
  This is a long text string that has whitespace and I would like to cut based on whitespace between words.
</div>

关于javascript - 如果文本太长,如何用空格分隔文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434798/

相关文章:

javascript - 最好的动画算法?

javascript - 钛 : uncaught type error

javascript - 如何从元素获取窗口属性

javascript - 如何制作网球从水平表面弹起的动画

css - Bootstrap 徽章的自定义形状

html - 使用绝对定位元素进行垂直和水平定位

javascript - Bootstrap 3 桌面粘滞菜单滚动

javascript - 如何将鼠标悬停在 Javascript 中的矩形上?

javascript - html 表单 - `required` 属性未通过 onclick 确认触发

javascript - 仅在切换类(class)时显示标题和段落?