JavaScript中文本框焦点时边框变色


例子

 代码如下 复制代码

function   appendit() 
  { 
  var   nodes   =   document.getElementsByTagName("INPUT"); 
  for   (var   i=0;   i<nodes.length;   i++) 
  { 
  var   ctype   =   nodes[i].getAttribute("type"); 
  if   (ctype   ==   'text') 
  { 
  nodes[i].onfocus   =   function   ()   {   this.style.backgroundColor='#33FF00';   } 
  nodes[i].onblur   =   function   ()   {   this.style.backgroundColor='#3366FF';   } 
  } 
  } 
  } 

测试:

<input type="text" size="36" name="title" />

再看一个例子

点击边框变色的文本框,鼠标点击文本框将要输入的时候,文本框自动变色高亮显示,非常有视觉效果,让文本框变漂亮了许多

 代码如下 复制代码

<!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="textml; charset=utf-8" />
<title>输入框点击时边框变色效果</title>
</head>
<body>
<script type="text/javascript">
// focusClass : 获取焦点时的样式
// normalClass : 正常状态下的样式
function focusInput(focusClass, normalClass) {
    var elements = document.getElementsByTagName("input");
    for (var i=0; i < elements.length; i++) {
        if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset") {
            elements[i].onfocus = function() { this.className = focusClass; };
            elements[i].onblur = function() { this.className = normalClass||''; };
        }
    }
}
</script>
<script type="text/javascript">
window.onload = function () {
    focusInput('focusInput', 'normalInput');
}
</script>
请输入姓名:<input type="text" class="normalInput" />
<style type="text/css">
.normalInput {border:1px solid #ccc;}
.focusInput {border:1px solid #FFD42C;}
</style>
</body>
</html>

在火狐下也有效,只不过火狐和chrome下,这两款浏览器默认会自动会输入框添加点击效果,所以有时候看不清,IE下表现突出。

把现在流行的基于jquery实现方法给各位介绍一个例子

 代码如下 复制代码

Html code:

<div class="fun">

   <h1>文本框聚焦清空默认值边框变色,离开焦点添加默认值</h1>

   <div class="text">

               <input  type="text" class="inputText" value="2222" />

               <input  type="text" class="inputText" value="2222" />

               <input  type="text" class="inputText ss" value="82747" />

               <input  type="text" class="inputText" value="094727" />

               <input  type="text" class="inputText" value="249049" />       

               <input  type="text" class="inputText" value="333" />

               <input  type="text" class="inputText" value="82747" />

   </div>

</div>

Css code

:

这个是基于jquery库的啊,请自己加上啊。

 代码如下 复制代码

<style type="text/css">

.fun{margin:0 auto;width:1000px;overflow:hidden;box-shadow:0 3px 6px rgba(0,0,0,0.1);border:solid 1px #ccc;font-family:Microsoft YaHei;overflow:hidden;}

.inputText{border:solid 1px #ccc;height:40px;width:200px;line-height:40px/9;padding:0 2px;box-shadow:inset 0 0 4px rgba(0,0,0,0.1);margin:10px 0;outline:none;font-family:arial;font-weight:700;text-indent:5px;color:#1C1C1C;display:inline-block;}

.inputText:focus{border:solid 1px #1398FF;box-shadow:0 0 5px rgba(0,192,255,0.4);}

.text{padding:15px 0 15px 75px;}

h1{text-align:center;padding:10px 0;margin:0;background:-webkit-linear-gradient(#fcfcfc,#f9f9f9) repeat;background:-moz-linear-gradient(#fcfcfc,#f9f9f9) repeat;border-bottom:solid 1px #ccc;font-weight:400;text-shadow:1px 1px 3px #fff;}/*Css3背景渐变*/

</style>

Js  code:

<script type="text/javascript">

// JavaScript Document

$(document).ready(function(){

function input()

{

//each遍历文本框

$(".inputText").each(function(){

var $val=this.value;//保存当前文本框的值

var $ss=$(".ss").val();

$(this).focus(function(){

//获得焦点时,如果值为默认值,则清空文本框的值

if (this.value== $val){

this.value="";

}

});

$(this).blur(function() {

//失去焦点时,如果值为空,则重新加上文本框默认值

if (this.value==""){

this.value=$val;

}

});

});

}

input();

})

</script>

时间: 2024-07-31 07:34:52

JavaScript中文本框焦点时边框变色的相关文章

javascript中文本框中输入法切换的问题_javascript技巧

可以通过设置ime-mode在页面输入控件中关闭输入法 希望关闭输入法:< input style ="ime-mode:disabled" > 希望使用当前输入法:<input   style="ime-mode:active"> 当某个文本域不需要中文输入的时候,我们可以通过设置 ime-mode 属性值为 inactive 或者 disabled,减少错误的可能性.同样的我们可以设置那些需要中文输入的文本域的 ime-mode 属性值为

JavaScript实现文本框中默认显示背景图片在获得焦点后消失的方法

  本文实例讲述了JavaScript实现文本框中默认显示背景图片在获得焦点后消失的方法.分享给大家供大家参考.具体如下: html代码: ? 1 2 3 4 5 6 7 8 9 <form name="searchform" id="search-form"> <div> <b>Search</b> <input type="text" name="txtInput" t

搜索文本框焦点离开时文本位置跳动问题解决方法

文本框焦点离开时会出现文本位置跳动问题,想必大家都有遇到过吧,其实解决很简单,只要固定宽高便可解决,遇到类似情况的朋友不妨参考下   搜索文本框焦点离开时设置文本,位置跳动问题   复制代码 代码如下: //搜索文本框 $("#txtSearch").focus(function () { if ($(this).val() == "请输入搜索关键字") { $(this).val(""); $(this).css("color&quo

focus-extjs编码中文本框获得焦点后无法进行其他交互

问题描述 extjs编码中文本框获得焦点后无法进行其他交互 events:[{id : 'code', event : 'blur', fn : 'doCheckCode'}], doCheckCode:function(textField,blur,eOpts){ var view = this.view; var value = view.getCmp("code").getValue(); if(("NEW"==view.uistatus)&&

IOS开发中取消文本框输入时的小键盘

  这篇文章主要介绍了IOS开发中取消文本框输入时的小键盘,需要的朋友可以参考下 首先在Interface Builder中选择TextFields,然后在Text Field Attributes中找到Text Input Traits,选择Return Key为done.OK 定义方法 - (IBAction) textFieldDoneEditing:(id)sender; //按下Done键关闭键盘 实现方法 代码如下: //按完Done键以后关闭键盘 - (IBAction) text

文本框焦点-WPF文本框获取焦点时打开外部程序后焦点失去,如何重新获取焦点

问题描述 WPF文本框获取焦点时打开外部程序后焦点失去,如何重新获取焦点 因为是触摸屏,所以做了一个外部键盘程序,但是在文本框获取焦点打开键盘程序时,焦点失去,文本框如何重新获取焦点 解决方案 使用LostKeyboardFocus方法 在msdn上就查找到: LostKeyboardFocus 在键盘焦点不再位于此元素上时发生 LostFocus 在此元素失去逻辑焦点时发生 IsFocused 获取一个值,该值确定此元素是否具有逻辑焦点 Focus 尝试将焦点设定到此元素上 如果键盘焦点和逻辑

javascript 前端-javascript中文本输入框通过bind方法绑定得到焦点和失去焦点时间

问题描述 javascript中文本输入框通过bind方法绑定得到焦点和失去焦点时间 通过id找到文本输入框,将该文本输入框在JavaScript中通过bind方法绑定得到焦点和失去焦点事件,不要在html中实现! 解决方案 $("input[@type=text]").bind({"focus":function(){alert(new Date())},"blur":function(){alert(new Date()}}) 解决方案二:

文本框字符离边框的间距和gdi+中drawstring能调到一样吗?

问题描述 文本框字符离边框的间距离文本框的边是3个像素,但是gdi+中drawstring函数却是根据字体的不同离边框的间距而不同,有什么办法让drawstring画的字符,离边框也是3个像素. 解决方案 解决方案二:up

javascript设置文本框光标的方法实例小结_javascript技巧

本文实例总结了javascript设置文本框光标的方法.分享给大家供大家参考,具体如下: 对于text //得到光标位置 function getCaret(textbox) { var control = document.activeElement; textbox.focus(); var rang = document.selection.createRange(); rang.setEndPoint("StartToStart",textbox.createTextRange