Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

javascript - Adding event handler to an iframe using JQuery

I want to assign a keydown event handler to an iframe. Something similar to the pure JS:

document.getElementById('iframe_id').contentWindow.addEventListener('keydown',funcName, true);

I tried:

$(document.getElementById('iframe_id').contentWindow).keydown( function() {
   // my func
});

But it does not work.. Please help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

contentWindow is the iframe's window object. You want the iframe's document instead:

$(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
    // my func
});

Note that I am not sure how jQuery reacts to elements from other windows/frames.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...