创建一个漂亮的input form标签

经常看到有些网站输入框或者搜索框有个底色文字,当你吧鼠标放上去的时候就没有了,离开就又有了,这个是怎么实现的呢?经过我的一番研究,终于知道他是如何实现的了使用 jquery控制实现的,代码如下

复制代码
 1 $('input').focus(function() {
 2 
 3     if($(this).val() == "Enter your email here")
 4         $(this).val('');
 5 
 6 }).blur(function() {
 7 
 8     if($(this).val() == "")
 9         $(this).val('Enter your email here');
10 
11 });
复制代码

不用我解释,相信你也能看到代码是什么意思

html代码如下

1 <form>
2     <label class="username-label" for="username">Username</label>
3     <input type="text" name="username" class="username">
4     <label class="password-label" for="password">Password</label>
5     <input type="password" name="password" class="password">
6 </form>

CSS代码如下

.username-label, .password-label {
    position: absolute;
    margin: 9px 9px 9px 12px;
}

javascript代码如下

复制代码
 1 $('.username-label, .password-label').animate({ opacity: "0.4" })
 2     .click(function() {
 3         var thisFor    = $(this).attr('for');
 4         $('.'+thisFor).focus();
 5 });
 6 
 7 $('.username').focus(function() {
 8 
 9     $('.username-label').animate({ opacity: "0" }, "fast");
10 
11         if($(this).val() == "username")
12             $(this).val() == "";
13 
14     }).blur(function() {
15 
16         if($(this).val() == "") {
17             $(this).val() == "username";
18             $('.username-label').animate({ opacity: "0.4" }, "fast");
19         }
20     });
21 
22 $('.password').focus(function() {
23 
24     $('.password-label').animate({ opacity: "0" }, "fast");
25 
26         if($(this).val() == "password") {
27             $(this).val() == "";
28         }
29     }).blur(function() {
30 
31         if($(this).val() == "") {
32             $(this).val() == "password";
33             $('.password-label').animate({ opacity: "0.4" }, "fast");
34         }
35 });
复制代码
posted @   创想中国(羲闻)  阅读(1027)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示