简体   繁体   English

jQuery:Windows Google Chrome浏览器不会在iframe中触发事件

[英]jquery: Windows Google Chrome does not trigger event in iframe

So, I'm working on a WYSIWYG editor with an already setup Grammar, etc. I've been tasked to add copy and paste functionality for cross program. 因此,我正在使用已设置的Grammar等进行WYSIWYG编辑器的工作。我的任务是为跨程序添加复制和粘贴功能。 The code I have works in Google Chrome on Ubuntu, Firefox in Ubuntu and Windows, but not in Chrome on windows. 我拥有的代码可在Ubuntu的Google Chrome,Ubuntu和Windows的Firefox中工作,但在Windows的Chrome中无法工作。

I've tracked down the problem to something basic. 我已将问题归结为基本问题。 When the paste command is sent (through the Ctrl + V) event, I immediately change focus to a hidden, contenteditable, iframe, and trigger the event there. 发送粘贴命令(通过Ctrl + V)事件后,我立即将焦点更改为隐藏的,可编辑的iframe,并在那里触发该事件。 I then wait for it to bubble up and then handle parsing and pasting. 然后,我等待它冒泡,然后处理解析和粘贴。

this.pasteOperation = function(event, controller) {
     this._cutpastearea.getEditable().focus().trigger(event);
     setTimeout(function() { controller.clipboardControl().handlePaste(); }, 1);
},

(Where getEditable gets the contenteditable section of the iframe) (在哪里getEditable获取iframe的contenteditable部分)

Now, if I reveal the iframe, and disable the set timeout, I find that focus has been grabbed, but nothing has been pasted. 现在,如果我显示iframe并禁用了设置超时,则会发现焦点已被抓住,但是没有粘贴任何内容。 If I proceed to manually paste it in, and then call the function, everything works. 如果我继续手动粘贴它,然后调用该函数,则一切正常。

So why does this not trigger the event? 那为什么不触发事件呢?

Edit: Raised the issue at Chromium Issues as well. 编辑:在Chromium Issues也提出了这个问题。 It seems to be an iframe only issue. 看来这只是iframe的问题。

Found a work around. 找到了解决方法。 Instead of calling trigger, call this: 与其调用触发器,不如调用它:

$.fn.forwardEvent = function(event) {
    this.each(function() {
        if (this.dispatchEvent) {
            if (event.originalEvent) {
                event = event.originalEvent
            }
            try {
                this.dispatchEvent(event);
            } catch(error) {
                $(this).trigger(event);
            }
        }
        else {
            $(this).trigger(event);
        }
    });
    return this;
};

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

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