admin管理员组文章数量:1326345
I have a jsp page with frames
<%@ include file="/includes/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" ".dtd">
<html>
<head>
<title>Wele</title>
<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(this).keydown(function(e) {
if(e.keyCode==27){
alert("escape pressed");
e.preventDefault();
}
});
}
);
</script>
</head>
<frameset rows="42,*" frameborder="0" framespacing="0" id="framest">
<frame src="/xyz/abc.html" scrolling="no" name="frame1"/>
<frame src="/xyz/init.html" scrolling="no" name="frame2"/>
</frameset>
</html>
I am trying to capture escape key press. But this doesn't seem to work. On each individual frame html if I write the same code of capturing , it works perfectly fine.
What changes should I make in the code above so that I would write the keydown code just once which enables me to capture keydown on anywhere on page on any frame.
I have a jsp page with frames
<%@ include file="/includes/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3/TR/html4/frameset.dtd">
<html>
<head>
<title>Wele</title>
<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(this).keydown(function(e) {
if(e.keyCode==27){
alert("escape pressed");
e.preventDefault();
}
});
}
);
</script>
</head>
<frameset rows="42,*" frameborder="0" framespacing="0" id="framest">
<frame src="/xyz/abc.html" scrolling="no" name="frame1"/>
<frame src="/xyz/init.html" scrolling="no" name="frame2"/>
</frameset>
</html>
I am trying to capture escape key press. But this doesn't seem to work. On each individual frame html if I write the same code of capturing , it works perfectly fine.
What changes should I make in the code above so that I would write the keydown code just once which enables me to capture keydown on anywhere on page on any frame.
Share Improve this question asked Jan 6, 2011 at 8:56 RaksRaks 8703 gold badges11 silver badges28 bronze badges 1-
Bit unrelated, but
frames
are kind of deprecated now, I strongly suggest usingiframes
instead – Dan Commented Jan 6, 2011 at 10:47
2 Answers
Reset to default 5Remember a frame is a pletely new HTML page with a whole separate DOM, so jQuery doesn't include it in your bindings.
So you will need to bind to those documents also:
function keyDownHandler(e) {
if(e.keyCode==27){
alert("escape pressed");
e.preventDefault();
}
}
for (var id in window.parent.frames)
$(window.parent.frames[id].document).keydown(keyDownHandler);
$(document).keydown(keyDownHandler);
If the above doesn't work for you because of the id
:
for (var i = 0; i < window.parent.frames.length; i ++) {
var frame = window.parent.frames[i].document;
frame.addEventListener("keydown",keyDownHandler, true);
}
If you want to reference all frames, from the top level
for (var i = 0; i < top.window.frames.length; i ++) {
var frame = top.window.frames[i].document;
frame.addEventListener("keydown",keyDownHandler, true);
}
You can also try this:
Write your code in .js file and include file in all frames. This way you have to write function only onces and you can call it in all frames.
Hope this hepls.
本文标签: javascriptCapturing of keydown events in a html page with framesStack Overflow
版权声明:本文标题:javascript - Capturing of keydown events in a html page with frames - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742199126a2431629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论