javascript文本框获取焦点设置其样式代码

快乐打工仔 分类:实例代码

本章节分享一段代码实例,它实现了文本框获取焦点能够设置文本框样式的效果。

其实使用纯css也可以实现此功能,具体可以参阅css实现的文本框focus获取焦点设置样式代码实例一章节。

代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<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事件一章节。

回复

我来回复
  • 暂无回复内容