以下脚本实现:
button按钮默认回车(enter)事件。
如果想在页面中使用submit,则可以直接默认回车事件(enter),以下代码就不必看了。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <title>火狐和IE都支持的enter事件-www.jb200.com</title> <script src= "<a href=" http: //www.jb200.com/jb/jquery/" target="_blank" class="infotextkey">jquery</a>.1.3.2.js" type="text/<a href="http://www.jb200.com/js/" target="_blank" class="infotextkey">javascript</a>" language="javascript"></script> <script type= "text/javascript" > document.onkeydown = function (e) { var theEvent = window.event || e; var code = theEvent.keyCode || theEvent.which; if (code == 13) { $( "#but1" ).click(); } } $(document).ready( function () { $( "#but1" ).click( function () { alert( "我是enter事件," + "text值:" + $( "#text1" ).val()); }) $( "#but2" ).click( function () { alert( "我是Jquery事件" + "text值:" + $( "#text1" ).val()); }) }); </script> </head> <body> <input type= "text" id= "text1" /> <input type= "button" id= "but1" value= "确定(我是enter事件)" /> <input type= "submit" id= "but2" value= "确定(我是Jquery事件)" /> </body> </html> |