设为首页   |  加入收藏夹 快速导航:  热门文章  |  最新文章  |  梦想博客  
当前位置:编程之家 -> 文章频道 ->jquery 
站内搜索:  

jQuery应用-实现文本框获取焦点时改变样式

作者:kaixin110 来源:互联网 整理日期:2008-05-05

功能实现:
用户在输入文字时,如果能高亮显示正在输入的那个文本框的话,会更人性化些,下面就使用jQuery来实现。
实现原理:
在document加载完成后(ready),添加input的focus和blur事件,并进行增加和删除样式的操作。
代码示例:


<html>
<head><title></title>
<style type="text/css">
     .redBack{}{
         color:white;
         background:red;
         border:2px solid black;
  }
</style>
<script language="javascript" src="jquery-1.1.4.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function(){
$(input).focus(function(){
    $(this).addClass(redBack);
//alert("hello");
});
$(input).blur(function(){
    $(this).removeClass(redBack);
});
});
</script>
</head>
<body>
<input type="text" value="hello,shanzhu" id="myText">
<input type="text" value="hello,shanzhu" id="myText2">
<input type="text" value="hello,shanzhu" id="myText3">
<input type="text" value="hello,shanzhu" id="myText4">
<input type="text" value="hello,shanzhu" id="myText5">
<input type="text" value="hello,shanzhu" id="myText6">
</body>
</html>

说明:
$(this)表示正在操作的对象。