【问题标题】:One line only for contenteditable element一行仅用于 contenteditable 元素
【发布时间】:2013-10-09 10:35:49
【问题描述】:

我尝试通过使用contenteditable 提交一些标题来制作一个元素,这样我希望用户只输入/粘贴标题一行

$('.title').on('keypress',function(e) {
   var code = e.keyCode || e.which;
   if(code == 13) {
      e.preventDefault();
   }
}).on('paste',function(){
   var text = $(this).text();
   $(this).html(text).focus();
});

问题是paste 事件,当我粘贴一些文本时,我无法使用.focus()选择/指向 文本到最后一个字符。。 p>

我做错了什么?

【问题讨论】:

  • 可能duplicate
  • @Alex 使用 CSS ! ,好主意感谢链接:D

标签: javascript jquery contenteditable paste


【解决方案1】:

我现在有想法了……

jQuery :

$('.title').on('keypress',function(e) {
   var code = e.keyCode || e.which;
   if(code == 13) {
      e.preventDefault();
   }
}).on('paste',function(){
   $('br,p',this).replaceWith(' ');
});

CSS :(非请求)

.title br {display:none;}

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2017-04-15
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多