文本框自動獲得焦點

 <!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" />
<title>文本框自動獲得焦點</title>


</head>

<body>
<form id="form1" name="form1">
<input type="text" id="txt1" name="txt1" />
</form>

<form id="form2" name="form2">
<input type="text" id="txt2" name="txt2" />
</form>
</body>

 

<script language="javascript" type="text/javascript">
//注意:DOM在載入完成方可操作,也就是說本代碼不能放於<head></head>中,至少要放在所有<input type="text">之後
var texts = document.getElementsByTagName("input");

for (var i=0;i<texts.length;i++)
{
    
if (texts(i).type == "text")
    {
        texts(i).onmouseover 
= function () {this.focus();}
    }
}
</script>

</html>

發佈了30 篇原創文章 · 獲贊 5 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章