我尝试使用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()
选择/指向文字到最后一个字符
我做错了什么?
答案 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;}