【问题标题】:texarea inserting newlines when pasting text in chrometextarea在chrome中粘贴文本时插入新行
【发布时间】:2012-12-26 19:57:58
【问题描述】:

我在 chrome(linux 和 windows)中的 textareas 出现了一种奇怪的行为。

  1. 打开这个小提琴:http://jsfiddle.net/sonic1980/N4vUS/
  2. 将 3 行文本复制到 textarea 中。
  3. 每行之间出现多个换行符。

为什么?我怎样才能避免这种情况?

textarea 有这种风格:

textarea {
    white-space: nowrap;
    height: 300px;
    width: 250px;
}​

【问题讨论】:

  • 此外,Firefox 可以正确显示它们,而 IE9 则根本不显示换行符。

标签: html google-chrome textarea


【解决方案1】:

我过去曾在 Safari 上看到过这个问题,并认为这是 WebKit 的一个错误。我不知道它是否已针对当前的 Chrome 版本进行了修复,但似乎已针对 Safari 进行了修复。

https://groups.google.com/forum/?fromgroups=#!topic/codemirror/m0LeyKF6LcE

【讨论】:

    【解决方案2】:

    快速而肮脏的 jQuery 解决方法,但对我有用。
    这会捕获paste 事件并删除所有空行。

    $('#myTextArea').bind('paste', function(e){
        window.setTimeout(function(){
            var text = '';
            $($('#myTextArea').val().split('\n')).each(function(i,v){
                v = $.trim(v);
                if (v.length > 0) text+= v+'\n';
            });
            console.log(text);
            $('#myTextArea').val(text);
        },1);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-21
      • 2016-10-09
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多