简体   繁体   中英

stop keypress event inside iframe

I am stuck with stoping the ENTER key on a input inside a iframe.

This works, crossbrowser, within the same page:

$("#input").keypress(function (event) {
    console.log(event.which); //works good
    if (event.which == 13) {
        return false;
    }
});

This does not work stoping/catching the keypress event inside the iFrame.

$("#my_iframe").contents().keypress(function (event) {
    console.log(event.which); // nothing loged
    if (event.which == 13) {
        return false;
    }
});

What am I missing? is there a crossbrowser solution?

Fiddle

您在附加事件处理程序之前不必等待iframe加载。

As an addition to Stephen Thomas's answer, here's a nice article summing up what the problem is:

Attaching an event to a form's elements inside an iframe with a use of jQuery

However if you try to update your fiddle with new code, you'll most likely get an error, something along these lines:

Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "http://jsfiddle.net". Protocols, domains, and ports must match

Seems like jsfiddle uses two different servers for its internal workings and that is against the "Same Origin" policy - in short, you shouldn't be able to access DOM of iframes from different sites. As a measure of safety I guess. Though I don't know if you can actually skip it somehow. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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