HTML DOM focus()和blur()方法 輸入框鼠標指過自動消失鼠標離開顯示 js

定義和用法

focus() 方法用於在密碼域上設置焦點。

語法

passwordObject.focus()

實例

下面的例子可設置或移開密碼域上的焦點:

<html>
<head>
<script type="text/javascript">
function setFocus()
  {
  document.getElementById('password1').focus()
  }
function loseFocus()
  {
  document.getElementById('password1').blur()
  }
</script>
</head>
<body>

<form>
<input type="password" id="password1" value="thgrt456" />
<input type="button" οnclick="setFocus()" value="Set focus" />
<input type="button" οnclick="loseFocus()" value="Lose focus" />
</form>

</body>
</html>
實例2:

<!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>Untitled Document</title>

<script type="text/javascript">
       function chg(){
       document.getElementById("setfocus").value="";
       }
       function newChg(){
       document.getElementById("setfocus").value="請輸入關鍵字";
       }
</script>

</head>

<body>
<input type="text" name="name" size="15" class="input" id="setfocus" value="請輸入關鍵字" οnfοcus="chg()" οnblur="newChg()">

</body>
</html>

實例3

<!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>Untitled Document</title>

<script type="text/javascript">
function $(ID) {return document.getElementById(ID);}
function chg(){
$("setfocus").value="";
}
function newChg(){
    if($("setfocus").value==""){
        $("setfocus").value="請輸入關鍵字";
        }
    else{return $("setfocus").value }
}
</script>

</head>

<body>
<input type="text" name="name" size="15" class="input" id="setfocus" value="請輸入關鍵字" οnfοcus="chg()" οnblur="newChg()">

</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章