javascript文字框獲取焦點設定其樣式程式碼

admin發表於2017-04-08

本章節分享一段程式碼例項,它實現了文字框獲取焦點能夠設定文字框樣式的效果。

其實使用純css也可以實現此功能,具體可以參閱css實現的文字框focus獲取焦點設定樣式程式碼例項一章節。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script>
window.onload=function(){
  var otxt=document.getElementById("txt");
  otxt.onfocus=function(){
    this.style.backgroundColor="#ccc";
    this.style.color="green";
  }
  otxt.onblur=function(){
    this.style.backgroundColor="";
    this.style.color="";
  }
}
</script>
<head>
</head>
<body>
<input id="txt" type="text" value="螞蟻部落歡迎您"/>
</body>
</html>

上面的程式碼實現了我們的要求,程式碼比較簡單,更多內容可以參閱相關閱讀。

相關閱讀:

(1).onfocus事件可以參閱javascript focus事件一章節。

(2).onblur事件可以參閱javascript blur事件一章節。

相關文章