为什么JQuery keydown适用于窗口而不适用于文本框?
问题描述:
为什么这样做:
$(window).keydown(function(event){
alert(event.keyCode);
});
但不是这样:
$('#ajaxSearchText').keydown(function(event){
alert(event.keyCode);
});
我正在使用Firefox 3进行测试. 有趣的是,它们都不在IE7中工作.
I'm testing with Firefox 3. Interestingly, neither of them work in IE7.
答
在Chrome,IE7和Firefox 3.0.3中进行了检查.可以正常工作. jQuery 1.2.6版.
Checked this in Chrome, IE7 and Firefox 3.0.3. Works as it should. jQuery version 1.2.6.
<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript">
$(function()
{
$("#ajaxSearchText").keydown(function(event)
{
alert(event.keyCode);
});
});
</script>
</head>
<body>
<input type="text" id="ajaxSearchText"></input>
</body>
</html>