一行仅适用于满足要素

时间:2013-10-09 10:35:49

标签: javascript jquery contenteditable paste

我尝试使用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() 选择/指向文字到最后一个字符

我做错了什么?

1 个答案:

答案 0 :(得分:4)

我现在有了想法......

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;}