【问题标题】:IE - how to preserve white space in textarea, inputting with javascript stringIE - 如何在 textarea 中保留空白,使用 javascript 字符串输入
【发布时间】:2011-10-20 23:09:47
【问题描述】:

FF 和 Chrome 表现良好,但 IE (8) 没有在我放入 TEXTAREA 的格式化代码中保留空白

<textarea id="vdt_table_textarea" style="white-space: pre"></textarea>

var vdt_demo_table_string = '&lt;table id=&quot;example&quot; class=&quot;display&quot;&gt;\n\
  &lt;thead&gt;\n\
    &lt;tr&gt;\n\
      &lt;th&gt;Rendering engine&lt;/th&gt;\n\
      &lt;th&gt;Browser&lt;/th&gt;\n\
      &lt;th&gt;Platform(s)&lt;/th&gt;\n\
      &lt;th&gt;Engine version&lt;/th&gt;\n\
      &lt;th&gt;CSS grade&lt;/th&gt;\n\
    &lt;/tr&gt;\n\
  &lt;/thead&gt;\n\
  etc....
';

$(document).ready(function() {
  $('#vdt_table_textarea').html(vdt_demo_table_string.replace(' ', '&nbsp;'));
});

如何让 IE 尊重我的权威?!

【问题讨论】:

    标签: html internet-explorer textarea whitespace


    【解决方案1】:

    CSS

    textarea{
    white-space:pre;
    }
    

    或者

    textarea{
    white-space:pre-wrap;
    }
    

    【讨论】:

      【解决方案2】:

      不知道为什么你的方式行不通,但这样做:

      $('#vdt_table_textarea').html(vdt_demo_table_string.replace(/ /g, '&nbsp;'));
      

      【讨论】:

      • 优秀。谢谢。指向你。我决定走另一条路,只使用 tinymce 作为源代码,并允许对表格进行可视化编辑。
      • 相信你会发现string.replace在做字符串替换的时候只替换了第一次出现的地方。
      • @RogerWillcocks 这就是最后的 g 的含义,我相信它代表全球。
      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      相关资源
      最近更新 更多