简体   繁体   English

为什么JQuery和Javascript DOM方法去除空格?

[英]Why is it that JQuery and the Javascript DOM methods strip whitespace?

I'm trying to write a chatroom in Javascript. 我正在尝试用Javascript编写聊天室。 It seems as if you send hello in the message submission box, it places hello in the message log on everybody's message log. 似乎您在消息提交框中发送了hello ,它将hello放在每个人的消息日志上的消息日志中。 I have also tried to use the native DOM methods, and the same thing happens there too. 我也尝试过使用本机DOM方法,同样的事情也发生在这里。

The Javascript to place the message in the log is like this: 将消息放入日志的Javascript如下所示:

socket.on('chat message', function(msg){
        $('#messages').append($('<li style="background: #ffffff;">' + msg + '</li>'))
})

The HTML of where it would be placed is like this: HTML的放置位置如下所示:

<ul id="messages">
</ul>

Can somebody please tell me why this happens, and how I could keep this from happening. 有人可以告诉我为什么会发生这种情况,以及如何阻止这种情况发生。

Browsers don't always display breaking whitespace when showing text, you can change that with CSS 浏览器在显示文本时并不总是显示空白,您可以使用CSS进行更改

#messages li {
  white-space: pre;
}

The valid values are as follows 有效值如下

Value    |  Description
_________|_________________________________________________________________________
normal   |  Sequences of whitespace will collapse into a single whitespace. 
         |  Text will wrap when necessary. This is default.
_________|_________________________________________________________________________
nowrap   |  Sequences of whitespace will collapse into a single whitespace. 
         |  Text will never wrap to the next line. 
         |  The text continues on the same line until a <br> tag is encountered.
_________|_________________________________________________________________________
pre      |  Whitespace is preserved by the browser. 
         |  Text will only wrap on line breaks.  
         |  Acts like the <pre> tag in HTML.
_________|_________________________________________________________________________
pre-line |  Sequences of whitespace will collapse into a single whitespace.
         |  Text will wrap when necessary, and on line breaks.
_________|_________________________________________________________________________
pre-wrap |  Whitespace is preserved by the browser. 
         |  Text will wrap when necessary, and on line breaks.
_________|_________________________________________________________________________
initial  |  Sets this property to its default value.
_________|_________________________________________________________________________
inherit  |  Inherits this property from its parent element.
_________|_________________________________________________________________________

 var msg = "hello"; $('#messages').append($('<li style="background: #ffffff;">' + msg + '</li>')); $('#messages2').append($('<li style="background: #ffffff;">' + msg + '</li>')); 
 #messages li { white-space: pre } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="messages"><li>With white-space</li></ul> <ul id="messages2"><li>Without white-space</li></ul> 

您应该将msg附加到pre标签,这将阻止浏览器格式化文本:

$('#messages').append($('<li style="background: #ffffff;"><pre>' + msg + '</pre></li>'))

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

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