小白教程
所有教程
关于
Search
172.70.131.176
172.70.131.176
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Event keydown”的源代码
本页内容
上一节:
Event_hover
下一节:
Event_keypress
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:keydown()}}[[Category:jquery ref events|32]] = jQuery keydown() 方法 = [[jquery ref events|jQuery 事件方法]] 当键盘键被按下时,设置 <input> 字段的背景颜色: <sample title="" desc="" lang="html" hererun="1"> $("input").keydown(function(){ $("input").css("background-color","yellow"); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").keydown(function(){ $("input").css("background-color","yellow"); }); $("input").keyup(function(){ $("input").css("background-color","pink"); }); }); </script> </head> <body> 输入你的名字: <input type="text"> <p>在以上输入框中输入你的名字。在按键按下后输入框背景颜色会改变。</p> </body> </html> </run> == 定义和用法 == 与 keydown 事件相关的事件顺序: # keydown - 键按下的过程 # [[event keypress|keypress]] - 键被按下 # [[event keyup|keyup]] - 键被松开 当键盘键被按下时发生 keydown 事件。 keydown() 方法触发 keydown 事件,或规定当发生 keydown 事件时运行的函数。 '''提示:'''请使用 [[event which|event.which]] 属性来返回哪个键盘键被按下。 == 语法 == 触发被选元素的 keydown 事件: $(selector).keydown() <run name=""> <!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").keydown(function(){ $("input").css("background-color","lightblue"); }); $("input").keyup(function(){ $("input").css("background-color","lavender"); }); $("#btn1").click(function(){ $("input").keydown(); }); $("#btn2").click(function(){ $("input").keyup(); }); }); </script> </head> <body> <input type="text"> <p><button id="btn1">Trigger keydown event for input field</button></p> <p><button id="btn2">Trigger keyup event for input field</button></p> </body> </html> </run> 添加函数到 keydown 事件: $(selector).keydown(function) <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").keydown(function(){ $("input").css("background-color","yellow"); }); $("input").keyup(function(){ $("input").css("background-color","pink"); }); }); </script> </head> <body> 输入你的名字: <input type="text"> <p>在以上输入框中输入你的名字。在按键按下后输入框背景颜色会改变。</p> </body> </html> </run> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | function | 可选。规定 keydown 事件触发时运行的函数。 |} == 更多实例 == [[try.php?filename=tryjquery_event_which|判断哪个键被按下]] 如何使用 event.which 属性来判断哪个键被按下。 [[jquery ref events|jQuery 事件方法]]
返回至“
Event keydown
”。
上一节:
Event_hover
下一节:
Event_keypress