javascript 键盘事件总结
在进入正题前,我们看一下浏览器对于键盘的一些默认事件,这有助于我们用javascript截获键盘事件。
在form中, submit的快捷键是 enter,reset的快捷键是 esc。不过在
IE6,safari4,ff3.5,opera10,chrome中
,按Enter,不但激发form的submit事件,同时也会激发
提交按钮的onclick,激发顺序为提交按钮的 onclick → form 的 onsubmit。
<html
dir="ltr" lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>键盘事件</title>
</head>
<body>
<h3>键盘事件</h3>
<form onsubmit="alert('Form is submiting');return false;">
<p><input type="text" value="将焦点聚焦于文本域中,然后按回车键或Esc键"
/></p>
<p><input type="submit" onclick="alert('submit button is
clicked');" value="submit"/>
<input type="reset" onclick="alert('reset button is clicked');"
value="reset" />
</p>
</form>
</body>
</html>
运行代码
不过并不止提交按钮会激发form的submit事件,连同上面的归纳如下:
如果表单里有一个type="submit"的按钮,回车键生效。
如果表单里只有一个type="text"的input,不管按钮是什么type,回车键生效。
如果按钮不是用input,而是用button,并且没有加type,IE下默认为type=button,FX默认为
type=submit。
其他表单元素如textarea、select不影响,radio
checkbox不影响触发规则,但本身在FX下会响应回车键,在IE下不响应。
type="image"的input,效果等同于type="submit"。不知道为什么会设计这样一种type,不推荐使用,应该
用CSS添加背景图合适些。
除了在按钮中绑定键盘事件外,浏览器还有一个accesskey 属性来指定链接的快捷键。注意 accesskey
的设置如果和浏览器的菜单相同,会优先于菜单。在IE中,快捷键是 alt + 设置的键值,FF是Alt+Shift+ 设置的键值。 在IE
中,a元素的 accesskey 只是使焦点转移到链接上,并不等同于点击,FF 中则相当于点击。与他对比的是,input
相关文档:
Dynamic Script Elements 动态脚本元素
The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrie ......
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
&nb ......
Introduction
This article is about passing data between VB.NET/C# WinForms and JavaScript.
Before reading further, let me warn you that this article is not about ASP.NET. Concepts covered in the article are applied to Desktop applications.
Background
I was working on a project which required dat ......
页面提交数据一般有两种方法:get,post。post就是所谓的form提交,使用视图;get是通过url提交。
Get方法一般用后台代码(如asp,asp.net)获得参数,代码很简单:Request.QueryString["id"];即可获取。
有些时候需要直接在前台获取url参数,要用到javascript,js没有直接获取url参数的方法,那么,我们如何通过js ......