简体   繁体   English

jQuery在内容可编辑的div中输入粘贴

[英]jquery on input paste in contenteditable div

There is a contenteditable div as a textarea in my html page: 我的html页面中有一个contenteditable div作为textarea:

<div id="txt" contenteditable="true"></div>

I detect the user's input through jquery "input paste" 我通过jQuery“输入粘贴”检测到用户的输入

$('#txt').on('input paste',function(event){
    alert("ok"); 
});

It works fine in chrome, but not working in IE. 它在chrome中工作正常,但在IE中不起作用。 Could anyone tell me what happened? 谁能告诉我发生了什么事? Thanks~~ 谢谢~~

IE is returning event.type as paste IE浏览器返回event.type作为paste

$('#txt').on('input paste',function(event){
   if(event.type=='paste' || event.type=='input'){
       alert("ok"); 
   }  
});

EDIT. 编辑。 For some reason I only tested with paste. 由于某种原因,我只测试了粘贴。

$('#txt').on('keyup paste',function(event){
   alert("ok"); 
});

I think this is a problem which is IE specific. 我认为这是特定于IE的问题。 Alternate solution is to use focus, keyup event and blur event. 替代解决方案是使用焦点,抠像事件和模糊事件。 You can compare old and new text to see if change is there in text or not. 您可以比较新旧文本,以查看文本中是否存在更改。

Just found a SO thread where given answer lined up with my answer; 刚发现一个SO线程,给定答案与我的答案对齐; and a link to original SO thread. 以及指向原始SO线程的链接。

$('#txt').on('focus', function() {
  before = $(this).html();
}).on('blur keyup paste', function() { 
  if (before != $(this).html()) { $(this).trigger('change'); }
});

$('#txt').on('change', function() {alert('changed')});

SO Thread SO线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM