【问题标题】:Is there any event can tell me that IE has finished its contenteditable action?是否有任何事件可以告诉我 IE 已完成其 contenteditable 操作?
【发布时间】:2013-06-20 10:37:59
【问题描述】:

我正在开发基于 contenteditable 的文本编辑器,试图修改 IE 的 contenteditable 行为。

例如:按回车键时插入<br /> 不是新的<p> 元素

我发现 IE 在键入时在 keyupkeydown 之间完成了它的 contenteditable 行为。所以我可以在这两个事件触发时添加我的修改。

但是当来到 Paste 时,它不起作用。

案例是: 我想像这样粘贴几个段落的文本块

paragraph 1

another paragraph

and so on

IE 会自动在新的<p> 标签中嵌入段落。例如,如果我想将class 添加到这些<p> 元素中,我需要知道这些元素是何时创建的。

我试过onpastenodeInserted。他们都在变化发生之前被解雇了。 IE 完成工作后是否触发了任何事件?

【问题讨论】:

  • 与问题不完全相关,但请查看this jsFiddle。它包含我为 IE8 创建的 sn-p,我不记得粘贴文本时有什么麻烦。它似乎也适用于 IE10。

标签: javascript html internet-explorer contenteditable


【解决方案1】:

我一直在使用按键事件检测它。 如果我没记错的话,您可以从输入参数中获取按下的键和修饰符,以检测 Ctrl+v。

编辑 显然不是,我只是检查了一下。 JQuery 使用 'paste' 事件来做 id。

HTML

<p id="cont" contenteditable="true">This is an editable paragraph.</p>

Javascript

$("#cont").keypress(function() {
    console.log("Keypress " + Math.random());
});

$("#cont").bind('paste', function(e){
    console.log("Paste " + Math.random());
});

这是小提琴:http://jsfiddle.net/3djRp/3/

EDIT2 我借用了someone elses event combination 在内容更改后检测粘贴,但在某些浏览器中它会触发两次。

$cont = $("#cont");

$cont.data('oldval', $cont.text());
//console.log($cont.data('oldval'));

$cont.bind("propertychange input keyup paste", function(event){
    if($cont.data('oldval') != $cont.text()) {
        //console.log("old " + $cont.data('oldval'));
        //console.log("new " + $cont.text());
        $cont.data('oldVal', $cont.text());

        console.log('change: ' + $cont.text());
    }
});

【讨论】:

  • 谢谢,但这些事件在 IE 完成其对 contenteditable 元素的工作之前被触发。
  • 当我在 IE10 中测试时,paste fires before change happens 但是对于这个特定的问题,我找到了一种聪明的方法,通过在David Walsh's Blog 检测动画插入的元素检测
猜你喜欢
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多