将没有内容的 div 分成新行(无文本)
我从输入中获取输入的字符,并根据不同的样式对它们进行不同的设计,并在具有不同颜色和形状的容器中进行渲染。
我想要实现的目标是,当用户在输入中键入空格时,换行符,因此一行中有 2 个 div,第二行有 7 个 div,另一行有 5 个 div,所有样式都不同,没有内容并且分开当输入空格时。
我知道溢出换行或空白,但它们不适用于没有文本的内容。我的所有元素都是空的div,从内容的角度来看都是相同的,甚至是空格,那么如何让它们在输入空格时换行呢?
我可以通过 Javascript 和 CSS 的某种组合来实现这一点吗?
I am taking typed characters from input and depending what it is styling them differently and rendering in a container with different colour and shape.
This what I am trying to achieve is when user types a space in the input, the line breaks, so you have 2 divs in one line, 7 divs in second line and 5 divs in another line, all styled differently, without content and separated when space typed.
I know about overflow-wrap or white-space but they don't work for content without text. All my elements are empty divs are the same from content point of view, even the space, so how can I make them to break into new line when the space is inputed?
Can I make it happen with some combination of Javascript and CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看到你的回答后,如果我理解正确的话。当你想要这样做时,在空格上换行不是更好吗:
旧答案:
要删除初始空格,或文本完成后的空格,你可以使用Javascript的修剪功能:
// 第二个变量从中删除初始和尾随空格您的输入。
有关使用修剪的更多信息,您可以查看 W3School 上的多个示例
After seeing your answer, and if I understand correctly. Isn't it better to break the line on space as you want to do this:
Old Answer:
To remove initial space, or space after text finishes, you can use Javascript's trim functionality:
// the second variable removes initial and trailing space from your input.
For more info about using trim, you can check multiple examples on W3School
如果您希望 div 排成一行并与空格用户输入分开,请使用 display:flex 作为容器。当用户输入是空格时,让 div 换行,如下例所示:
If you want to divs get in a line and break with space user input, use display:flex for container. and when user input was space, make a div breaks line like this example: