简体   繁体   English

如何在不使用CSS的情况下保留动态添加的javascript DOM元素中的空白?

[英]How to preserve whitespace in dynamically added javascript DOM element without using CSS?

When adding in text with small whitespace appended to it for alignment purposes the whitespace is trimmed off (the whitespace is added in c# so by the time it gets to front end Javascript it cannot be edited - it would be nice to just use some CSS to do this but it is not an option ). 当添加带有小空格的文本以进行对齐时,空白被修剪掉(空格被添加到c#中,所以当它到达前端时Javascript它无法编辑 - 只需使用一些CSS就可以了。这样做,但它不是一个选项 )。

Here is what I tried so far: 这是我到目前为止尝试的内容:

<div id="testDiv"></div>
<script type="text/javascript">
 var zlp = document.getElementById("testDiv");
 zlp.innerHTML = "hello                hello";
 var zzz = document.createTextNode("hello                hello");
 zlp.appendChild(zzz);
</script>

Both of which produce hello hello . 两者都产生hello hello

White space characters are usually collapsed in HTML (by default) . 空格字符通常在HTML中折叠(默认情况下)

You can replace it with the &nbsp; 您可以将其替换为&nbsp; entity: 实体:

var text = text.replace(/\s/g, '&nbsp;');

\\s will match any white space character, such as space, tab and new line. \\s将匹配任何空白字符,例如空格,制表符和新行。 If you only want to replace space, use / /g instead. 如果您只想替换空格,请改用/ /g

Other options which avoid string manipulation: 避免字符串操作的其他选项:

  • Put the text in a pre element . 将文本放在pre元素中
  • Set the CSS 2 white-space property to pre as @Esailija pointed out. 将CSS 2 white-space属性设置为pre@Esailija指出。 You can always add CSS properties dynamically to elements, they don't have to be specified in a style sheet. 您始终可以动态地向元素添加CSS属性,不必在样式表中指定它们。

White space is collapsed in HTML. HTML中的空格已折叠。 It's not a JS issue, the same would happen if you typed that manually in the HTML document. 这不是JS问题,如果您在HTML文档中手动输入,也会发生同样的问题。 You need to replace the spaces with &nbsp; 您需要用&nbsp;替换空格

zlp.innerHTML = "hello                hello".replace( / /g, "&nbsp;" );

use 采用

zlp.innerHTML = "hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hello";

Like everyone else just said. 就像其他人刚刚说的那样。

use a html tag 'pre' 使用html标签'pre'

Example: 例:

<pre>
A line
   A line with indent
</pre>

result: 结果:

A line
   A line with indent

暂无
暂无

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

相关问题 使用ajax在动态添加的dom元素的onclick上调用javascript函数 - calling javascript function on onclick of dynamically added dom element using ajax 如何将javascript函数绑定到DOM上动态添加的Element? - How to bind a javascript function to dynamically added Element on DOM? 如何访问Angular中动态添加的DOM元素? - How to access dynamically added DOM element in Angular? Using vanilla JavaScript, is it possible to add a DOM element and fade in using CSS transition, without using setTimeout or CSS animation? - Using vanilla JavaScript, is it possible to add a DOM element and fade in using CSS transition, without using setTimeout or CSS animation? 使用javascript中的DOM动态获取元素中的值 - get value in an element dynamically using DOM in javascript javascript / jquery - 如何获取尚未添加到dom的元素/ css类的宽度 - javascript/jquery - how to get the width of an element / css class that hasn't been added to dom yet 如何使用 Javascript DOM 获取没有 ID 或 Class 名称的元素 - How to get element without ID or Class Name using Javascript DOM 如何让动态添加的DOM元素保持(刷新后) - How to make the DOM element stay(after refresh), which was added dynamically 如何删除在 Javascript 中动态添加的元素 - How to remove an element that is added dynamically in Javascript 获取动态创建的元素(Javascript,DOM)的CSS宽度 - Getting CSS width of dynamically created element (Javascript, DOM)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM