用jquery實(shí)現(xiàn)輸入框獲取焦點(diǎn)消失文字
更新時(shí)間:2013年04月27日 14:48:48 作者:
文本框中經(jīng)常會(huì)有提示你輸入的信息,當(dāng)你點(diǎn)擊文本框,提示信息自動(dòng)消失,下面也為大家介紹下輸入框消失文字的方法,感興趣的朋友可以參考下
我們?cè)诘卿浘W(wǎng)站的時(shí)候,文本框中經(jīng)常會(huì)有提示你輸入的信息,當(dāng)你點(diǎn)擊文本框,提示信息自動(dòng)消失,當(dāng)文本框什么都沒(méi)有,而且失去焦點(diǎn)的時(shí)候,又有了提示文字。
1.原型開(kāi)發(fā),先做一個(gè)簡(jiǎn)單的:
我們首先需要一個(gè)html文件:
<html>
<head>
<title>input test</title>
<meta name="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
//這里放置css
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
//這里放置jquery代碼
</script>
</head>
<body>
<form method="POST" id="user" action="">
User Name:<input type="text" name="username" value="Enter your name" /><br/>
PassWord:<input type="password" name="password" value="Enter your password" />
<input type="submit" name="sub" value="login" />
</form>
</div>
</body>
</html>
下面加入jquery代碼:
我使用了click 和blur內(nèi)置事件類(lèi)型處理,而且,只是對(duì)username框有效(因?yàn)槊艽a框還有別的因素考慮)
<html>
<head>
<title>input test</title>
<meta name="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#username").click(
function(){
if($(this).val()=="Enter your name"){
$(this).val("");
}
})
$("#username").blur(
function(){
if($(this).val()=="")
{
$(this).val("Enter your name");
}
})
});
</script>
</head>
<body>
<div id="content">
<form method="POST" id="user" action="">
User Name:<input type="text" id="username" name="username" value="Enter your name" /><br/>
PassWord:<input type="password" name="password" value="Enter your password" />
<input type="submit" name="sub" value="login" />
</form>
</div>
</body>
</html>
2.做的更好
這樣基本的原型就寫(xiě)成了,但是這個(gè)原型有許多的不足:
1.也許可以對(duì)密碼框也使用這種方式,但是密碼框的type類(lèi)型是password,它不能顯示,何來(lái)提示文字?
2. if($(this).val()=="")這種寫(xiě)法我可以接受,但是 if($(this).val()=="Enter your name"),你不覺(jué)得這很...要是我就想輸這個(gè)呢...
3.提示文字用別的灰色的粗體表示,這樣交互性是不是更強(qiáng)?
4.既然想要用兩種字體表示,能不能把他們提取出來(lái)?寫(xiě)在.css里?這個(gè)是可以重用的啊!
解決辦法:
1.密碼框先讓它的type是text的,等到點(diǎn)擊了,我們?cè)僭O(shè)置成password
2.用個(gè)變量來(lái)表示是否要切換吧。
3.設(shè)置不同的css.
4.用attr("class","class1"),attr("class","class2")來(lái)切換class,而不是引用id.(也就是說(shuō)用.不用#)
下面是實(shí)現(xiàn):
<html>
<head>
<title>input test</title>
<style type="text/css">
.default {
font-weight:bold;
color:#787878;
}
.puton{
font-weight:normal;
color:black;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var b=true;
$("#username").click(
function(){
if(b==true){
$(this).val("");
$(this).attr("class","puton");
b=false;
}
}
)
$("#username").blur(
function(){
if( $(this).val()==""){
$(this).val("Enter your name");
$(this).attr("class","default");
b=true;
}
}
)
});
$(document).ready(function(){
var b=true;
$("#password").click(
function(){
if(b==true){
$(this).val("");
$(this).attr("type","password");
$(this).attr("class","puton");
b=false;
}
})
$("#password").blur(
function(){
if( $(this).val()==""){
$(this).val("Enter your password");
$(this).attr("type","text");
$(this).attr("class","default");
b=true;
}
}
)
});
</script>
</head>
<body>
<div id="content">
<form method="POST" id="user" action="">
User Name:<input type="text" id="username" class="default" name="username" value="Enter your name" /><br/>
PassWord:<input type="text" id="password" class="default" name="password" value="Enter your password" />
<input type="submit" name="sub" value="login" />
</form>
</div>
</body>
</html>
3.更多:
把css寫(xiě)到外部文件.
DRY原則!用插件來(lái)實(shí)現(xiàn).
我在下一篇博客去實(shí)現(xiàn).
author: aiqier
1.原型開(kāi)發(fā),先做一個(gè)簡(jiǎn)單的:
我們首先需要一個(gè)html文件:
復(fù)制代碼 代碼如下:
<html>
<head>
<title>input test</title>
<meta name="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
//這里放置css
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
//這里放置jquery代碼
</script>
</head>
<body>
<form method="POST" id="user" action="">
User Name:<input type="text" name="username" value="Enter your name" /><br/>
PassWord:<input type="password" name="password" value="Enter your password" />
<input type="submit" name="sub" value="login" />
</form>
</div>
</body>
</html>
下面加入jquery代碼:
我使用了click 和blur內(nèi)置事件類(lèi)型處理,而且,只是對(duì)username框有效(因?yàn)槊艽a框還有別的因素考慮)
復(fù)制代碼 代碼如下:
<html>
<head>
<title>input test</title>
<meta name="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#username").click(
function(){
if($(this).val()=="Enter your name"){
$(this).val("");
}
})
$("#username").blur(
function(){
if($(this).val()=="")
{
$(this).val("Enter your name");
}
})
});
</script>
</head>
<body>
<div id="content">
<form method="POST" id="user" action="">
User Name:<input type="text" id="username" name="username" value="Enter your name" /><br/>
PassWord:<input type="password" name="password" value="Enter your password" />
<input type="submit" name="sub" value="login" />
</form>
</div>
</body>
</html>
2.做的更好
這樣基本的原型就寫(xiě)成了,但是這個(gè)原型有許多的不足:
1.也許可以對(duì)密碼框也使用這種方式,但是密碼框的type類(lèi)型是password,它不能顯示,何來(lái)提示文字?
2. if($(this).val()=="")這種寫(xiě)法我可以接受,但是 if($(this).val()=="Enter your name"),你不覺(jué)得這很...要是我就想輸這個(gè)呢...
3.提示文字用別的灰色的粗體表示,這樣交互性是不是更強(qiáng)?
4.既然想要用兩種字體表示,能不能把他們提取出來(lái)?寫(xiě)在.css里?這個(gè)是可以重用的啊!
解決辦法:
1.密碼框先讓它的type是text的,等到點(diǎn)擊了,我們?cè)僭O(shè)置成password
2.用個(gè)變量來(lái)表示是否要切換吧。
3.設(shè)置不同的css.
4.用attr("class","class1"),attr("class","class2")來(lái)切換class,而不是引用id.(也就是說(shuō)用.不用#)
下面是實(shí)現(xiàn):
復(fù)制代碼 代碼如下:
<html>
<head>
<title>input test</title>
<style type="text/css">
.default {
font-weight:bold;
color:#787878;
}
.puton{
font-weight:normal;
color:black;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var b=true;
$("#username").click(
function(){
if(b==true){
$(this).val("");
$(this).attr("class","puton");
b=false;
}
}
)
$("#username").blur(
function(){
if( $(this).val()==""){
$(this).val("Enter your name");
$(this).attr("class","default");
b=true;
}
}
)
});
$(document).ready(function(){
var b=true;
$("#password").click(
function(){
if(b==true){
$(this).val("");
$(this).attr("type","password");
$(this).attr("class","puton");
b=false;
}
})
$("#password").blur(
function(){
if( $(this).val()==""){
$(this).val("Enter your password");
$(this).attr("type","text");
$(this).attr("class","default");
b=true;
}
}
)
});
</script>
</head>
<body>
<div id="content">
<form method="POST" id="user" action="">
User Name:<input type="text" id="username" class="default" name="username" value="Enter your name" /><br/>
PassWord:<input type="text" id="password" class="default" name="password" value="Enter your password" />
<input type="submit" name="sub" value="login" />
</form>
</div>
</body>
</html>
3.更多:
把css寫(xiě)到外部文件.
DRY原則!用插件來(lái)實(shí)現(xiàn).
我在下一篇博客去實(shí)現(xiàn).
author: aiqier
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)控制文字內(nèi)容溢出用省略號(hào)(…)表示的方法
- JQuery 控制內(nèi)容長(zhǎng)度超出規(guī)定長(zhǎng)度顯示省略號(hào)
- Jquery循環(huán)截取字符串的方法(多出的字符串處理成"...")
- jQuery(js)獲取文字寬度(顯示長(zhǎng)度)示例代碼
- jquery xMarquee實(shí)現(xiàn)文字水平無(wú)縫滾動(dòng)效果
- jQuery計(jì)算textarea中文字?jǐn)?shù)(剩余個(gè)數(shù))的小程序
- jQuery實(shí)現(xiàn)長(zhǎng)文字部分顯示代碼
- input 輸入框獲得/失去焦點(diǎn)時(shí)隱藏/顯示文字(jquery版)
- jquery實(shí)現(xiàn)微博文字輸入框 輸入時(shí)顯示輸入字?jǐn)?shù) 效果實(shí)現(xiàn)
- jQuery實(shí)現(xiàn)文字超過(guò)1行、2行或規(guī)定的行數(shù)時(shí)自動(dòng)加省略號(hào)的方法
相關(guān)文章
基于jQuery代碼實(shí)現(xiàn)圓形菜單展開(kāi)收縮效果
jquery圓形菜單展開(kāi)收縮效果是基于jquery和css3實(shí)現(xiàn)的,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02
清空元素html("") innerHTML="" 與 empty()的區(qū)別和應(yīng)用(推薦)
這篇文章主要介紹了清空元素html("")、innerHTML="" 與 empty()的區(qū)別和應(yīng)用,詳細(xì)介紹了三者之間的原理及應(yīng)用,需要的朋友可以參考下2017-08-08
jQuery實(shí)現(xiàn)點(diǎn)擊按鈕彈出可拖拽模態(tài)對(duì)話框完整實(shí)例【測(cè)試可用】
這篇文章主要介紹了jQuery實(shí)現(xiàn)點(diǎn)擊按鈕彈出可拖拽模態(tài)對(duì)話框的方法,結(jié)合完整實(shí)例形式分析了jQuery調(diào)用模態(tài)對(duì)話框的基本原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2023-04-04
jQuery獲取上傳文件的名稱(chēng)的正則表達(dá)式
在Web開(kāi)發(fā)中,經(jīng)常會(huì)涉及到文件上傳。文件上傳時(shí)通常都要驗(yàn)證文件的有效性,這個(gè)通常就要用正則表達(dá)式來(lái)判斷。2015-05-05
簡(jiǎn)單選項(xiàng)卡 js和jquery制作方法分享
這篇文章主要介紹了簡(jiǎn)單選項(xiàng)卡 js和jquery制作方法,需要的朋友可以參考下2014-02-02
JQuery實(shí)現(xiàn)table中tr上移下移的示例(超簡(jiǎn)單)
下面小編就為大家分享一篇JQuery實(shí)現(xiàn)table中tr上移下移的示例(超簡(jiǎn)單),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
JqueryMobile動(dòng)態(tài)生成listView并實(shí)現(xiàn)刷新的兩種方法
本篇文章主要是對(duì)JqueryMobile動(dòng)態(tài)生成listView并實(shí)現(xiàn)刷新的兩種方法進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03

