当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Jquery - Cannot set keydown event when put cursor in iframe (text editor)

问题描述:

I am trying to call a "keydown" event for textbox and texteditor, using some function with jquery (ex: press Alt+1 to lowercase, alt+2 to uppercase)

I'm using this js to set keydown event:

$(window).keydown(function (e) { alert("some thing"); });

That is OK with the textbox (

I'm trying to set the "keydown" event for my texteditor with js:

 window.onload = function () {

$('iframe').find('body').context.onkeydown = function () {

alert("aaaaaaaaaaaaaaaaaaaaaaa");

};

}

That js runs when my cursor is out of my text editor, when it is in my cursor in, then nothing runs.

This is Developer Console F12

http://i.imgur.com/7B0gdCM.jpg

I searched in google and stackoverflow a lot for a solution, but found nothing. Can anyone help me?

网友答案:

First of all look at same-origin policy article.

You are trying to access iframe content from the outer document. If your case satisfies same-origin policy rules (your iframe content if served from the same domain) then you will be able to operate on the iframe content DOM.

Anyway using jQuery from the outer document is not a good idea.

分享给朋友:
您可能感兴趣的文章:
随机阅读: