jQuery学习笔记7——单行文本框

本文介绍了一种使用jQuery为单行文本框添加焦点高亮效果的方法,通过简单的CSS样式和jQuery事件处理,实现了文本框获得及失去焦点时边框颜色和背景的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单行文本框获取和失去焦点改变样式.

 1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2<html xmlns="http://www.w3.org/1999/xhtml">
 3<head>
 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 5<title>单行文本框应用</title>
 6<script language="javascript" src="jquery-1.3.1.js"></script>
 7<style type="text/css">
 8ExpandedBlockStart.gifContractedBlock.gif* { margin:0; padding:0; word-break:break-all; }
 9ExpandedBlockStart.gifContractedBlock.gifbody { background:#fff; color:#333; font:12px/1.5em Helvetica, Arial, sans-serif; }
10ExpandedBlockStart.gifContractedBlock.gifh1, h2, h3, h4, h5, h6 { font-size:1em; }
11ExpandedBlockStart.gifContractedBlock.gif{ color:#2B93D2; text-decoration:none; }
12ExpandedBlockStart.gifContractedBlock.gifa:hover { color:#E31E1C; text-decoration:underline; }
13ExpandedBlockStart.gifContractedBlock.giful, li { list-style:none; }
14ExpandedBlockStart.gifContractedBlock.giffieldset, img { border:none; }
15ExpandedBlockStart.gifContractedBlock.gif.focus{ border:1px solid #f00;background:#fcc;}
16</style>
17</head>
18<body>
19<form action="#" method="POST" id="regForm">
20    <fieldset>
21        <legend>个人基本信息</legend>
22        <div>
23        <label for="username">名称:</label><input type="text" id="username"/>                        
24        </div>
25        <div>
26        <label for="pass">密码:</label><input type="password" id="pass"/>
27        </div>
28        <div>
29        <label for="msg">详细信息:</label><textarea id="msg"></textarea>
30        </div>
31    </fieldset>
32</form>
33</body>
34</html>

 添加jQuery事件

1ExpandedBlockStart.gifContractedBlock.gif$(function(){
2ExpandedSubBlockStart.gifContractedSubBlock.gif    $("input").focus(function(){
3        $(this).addClass("focus");
4ExpandedSubBlockStart.gifContractedSubBlock.gif    }
).blur(function(){
5        $(this).removeClass("focus");
6    }
);
7}
)   

 或者

1ExpandedBlockStart.gifContractedBlock.gif$(function(){
2ExpandedSubBlockStart.gifContractedSubBlock.gif    $(":input").focus(function(){
3        $(this).addClass("focus");
4ExpandedSubBlockStart.gifContractedSubBlock.gif    }
).blur(function(){
5        $(this).removeClass("focus");
6    }
);
7}
)   

 "input"只配合<input/>元素

 ":input"可以匹配所有<input>,<textarea><select><button>元素

转载于:https://www.cnblogs.com/guopei/articles/1521929.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值