Cannot lose focus from iframe when key is pressed

I have an iframe in my html page. I have added key events to my html document.

When I press "a" or "z", I can do something like addClass or removeClass.

So, in this block, when I try to add focus to the iframe, it is added. But with the key event, I cannot lose the focus (blur) of the iframe.

Here is a sample code:

    switch (myKey) {
        case 'a':
            $('div').append("<p>a pressed</p>");
            $("iframe").addClass("thick");
            $("iframe").focus();
            break;
        case 'z':
            $('div').append("<p>z pressed</p>");
            $("iframe").removeClass("thick");
            $("iframe").blur();
            break;
        default:
            //console.log('keycode', keycode);
    }

Can someone help me and tell me where this is wrong?

Fiddle - http://jsfiddle.net/hgXyq/70/

+3
source share
5 answers

DOM. , iframe ( ). , . - , .

...
$(document).on('keydown keyup keypress', documentKeys);
$('iframe').on('keydown keyup keypress', documentKeys); //This line was added
+3

iframe, iframe. - . , "z" iframe, - .

iframe , html5 postMessage iframe parent (iframe ). jquery, , postMessage .

+1

Firefox, Chrome. , Chrome . : http://www.quirksmode.org/dom/events/blurfocus.html

iframe, , - PDF:)), .

$(document).on()

$($("iframe").get(0).contentWindow.document).keypress(documentKeys);

. , iFrame () . , Localhost ( ).

JsFiddle : http://jsfiddle.net/pw8qb/3/

, !

+1

iframe, . .

"z", , .

0

, .

, !

,

, !

, ,

, :

setInterval/setTimeout. iframe, up arrow down arrow ( z), PDF, iframe up arrow down arrow! , , iframe, ! , !

, , , setInterval / setTimeout - , !

, iframe Internal:

This is done, get your link content (which you install on an iframe, for example here .pdf) in blob(), and use url.createobjecturl(blob)and set it like iframe source!

Now it iframeis under your control, and that ever the event that you want to add / remove can be achieved if you can set blob()how iframe src.

EXAMPLE

This is just a bad way to achieve this terrible task !!

In a way, this will help you ...

0
source

All Articles