问题描述
- jquery多层iframe绑定keyup
-
文件index.htm<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <iframe src="iframe.htm" name="index" style="width:400px;height:200px"></iframe> </body> </html>
文件iframe.htm
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script style="text/javascript" src="jquery.js"></script> </head> <body> <input type="text" id="text" value="" /> <input type="button" id="button" value="button" /> </body> <script> $(parent.document,document).keyup(function(e){ $('#text').val('p'+e.keyCode); }); $('#button').click(); </script> </html>
访问index.htm时,能正常捕捉键盘输入,但是当点击内框架iframe.htm使之获取焦点后,不能继续捕捉键盘输入,再点回父窗口又能正常。这是怎么回事,怎么解决?
解决方案
注意方法的参数:jQuery(expression,[context])
$(parent.document,document).keyup
==>
$(parent.document).add(document).keyup(function (e) {
$('#text').val('p' + e.keyCode);
});
时间: 2024-12-22 22:50:25