|
|
2#

楼主 |
发表于 2007-10-4 11:07:26
|
只看该作者

此函数为每一栏的onkeyup事件赋值,因此每次在栏中输入一个值,以检验其是否达到最大长度时,都要调用该函数。如果达到最大值,则光标按tab顺序移动到下一栏中。列表B中的源代码将此函数添加到上面的例子中。
列表B
<html><head>
<title>tabIndex Example 2</title>
<script type="text/javascript">
function checkLen(x,y) {
if (y.length==x.maxLength) {
var next=x.tabIndex
if (next<document.getElementById("frmTest").length) {
document.getElementById("frmTest").elements[next-1].focus();
} } }
</script></head>
<body>
<form name="frmTest">
<hr size="3" />
<div id="nameSection" tabIndex="1" style="background:silver; padding: 15px;">
First: <input id="idfname" size="50" name="firstName" tabIndex="2" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
Middle: <input id="idmname" size="50" name="middleName" tabIndex="3" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
Last: <input id="idlname" size="50" name="lastName" tabIndex="4" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
</div>
<hr size="3" />
<div id="addressSection" tabIndex="5" style="background:gold; padding: 15px;">
Address: <input id="idaddress" size="50" name="address" tabIndex="6" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
City: <input id="idcity" size="50" name="city" tabIndex="7" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
State: <input id="idlstate" size="50" name="state" tabIndex="8" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
Country: <input id="idcountry" size="50" name="country" tabIndex="9" maxlength="50" onkeyup="checkLen(this,this.value)" /><br />
</div>
<hr size="3" />
<div id="addressSection" tabindex=10 style="background:lightblue; padding: 15px;">
Home: <input id="idhome" size="50" name="homenumber" tabIndex="11" maxlength="16" onkeyup="checkLen(this,this.value)" /><br />
Work: <input id="idwork" size="50" name="worknumber" tabIndex="12" maxlength="16"
onkeyup="checkLen(this,this.value)" /><br />
Mobile: <input id="idlcell" size="50" name="cellnumber" tabIndex="13" maxlength="16" onkeyup="checkLen(this,this.value)" /><br />
</div>
<hr size="3" />
<div id="buttonsSection" tabindex=14 style="background:green;">
<input type="button" name="submitButton" tabIndex="-1" id="idSubmitButton" value="Submit" onfocus="document.write('submit form');" />
<input type="button" name="cancelButton" tabIndex="-1" id="idCancelButton" value="Cancel" />
</div></form></body></html>提高访问能力
为元素指定tabIndex可帮助经常使用键盘的用户更为方便地接触网络界面;非标准用户,如PDA、移动电话以及残疾人使用的屏幕阅读器也从中受益。任何扩充应用软件功能的事件都是有益的。
小事一桩
你可能要花许多时间来设计网络窗体,使其具有视觉吸引力,并能被后端服务器组件正确处理。但是,你可能忽略了要用非标准技术或浏览器对其进行测试。典型的例子就是放弃鼠标,而依靠键盘来导航窗体。HTML标准包含tabIndex特性,它允许你控制通过[Tab]键访问的项目。 |
|