---
title: "设置页面光标的位置"
author: "hecal"
date: "2009-08-25T10:26:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-12-07T18:09:35Z"
id: 1296500
url: "https://hecal.iteye.com/blog/1296500"
---

1 java代码里获取出错的控件的名字，放到request里。

if (isErr == true && null == request.getAttribute("checkResult")) {  
request.setAttribute("checkResult", "dateFrom");  
}

2 页面里添加javascript

&lt;script type="text/javascript"&gt;  
function setFocusOfUserList() {  
var checkResult = '&lt;%=request.getAttribute("checkResult") %&gt;';  
if (checkResult != 'null') {  
document.getElementById(checkResult).focus();  
} else {  
document.getElementById('gymDateFrom\_tl').focus();  
}  
}  
&lt;/script&gt;

3 页面的onload里调用。

&lt;body onload="setFocusOfUserList()"&gt;