应用扩展:普遍应用的案例就是在网站的登陆页中,为了更方便用户输入,进入页面后就定位到用户名密码的文本框,不用再动鼠标进行点击。增强了用户体验。
希望能够帮助到您。
1、方法:
$("html").die().live("keydown",function(event){
if(event.keyCode==13){
$(this).parent().next().find("input").facus()
}
})
2、代码如下
用自带的focus()就可以了
利用js中<input/>实现文本框默认获取输入焦点完整代码实现如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
//输入框获取鼠标焦点
function autoFocus(){
var pFocus = document.getElementById("password")
pFocus.focus()
pFocus.select()
}
</script>
</head>
<body onload="autoFocus()">
<div id="loginform">
<h1 style="font-size:1.5empadding:20px">输入密码</h1>
<form action="${pageContext.request.contextPath}/" method="post">
<input id="password" type="password" name="password">
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
扩展资料:
JavaScript 使我们有能力创建动态页面。事件是可以被 JavaScript 侦测到的行为。 网页中的每个元素都可以产生某些可以触发JavaScript函数的事件。比方说,我们可以在用户点击某按钮时产生一个 onClick 事件来触发某个函数。事件在 HTML 页面中定义 。
参考资料:
JavaScript官方API接口-:focus
百度百科-JavaScript
W3cschool-获得字段焦点