兩種方法實(shí)現(xiàn)文本框輸入內(nèi)容提示消失
更新時(shí)間:2013年03月17日 09:48:14 作者:
第一種方法:基于HTML5 input標(biāo)簽的新特性 - placeholder 。另外,x-webkit-speech 屬性可以實(shí)現(xiàn)語音輸入功能;第二種方法:
用span模擬,定位span,借助JS鍵盤事件判斷輸入,確定span里的內(nèi)容顯示隱藏
第一種方法:
基于HTML5 input標(biāo)簽的新特性 - placeholder 。另外,x-webkit-speech 屬性可以實(shí)現(xiàn)語音輸入功能。
<div><input type="email" name="email" spellcheck="false" placeholder="郵 箱" autofocus tabindex="1" x-webkit-speech></div>
<div><input type="password" name="password" placeholder="密 碼" tabindex="2"></div>
第二種方法:
用span模擬,定位span,借助JS鍵盤事件判斷輸入,確定span里的內(nèi)容顯示隱藏。
<!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="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#focus .input_txt").each(function(){
var thisVal=$(this).val();
//判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
//聚焦型輸入框驗(yàn)證
$(this).focus(function(){
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
});
})
$("#keydown .input_txt").each(function(){
var thisVal=$(this).val();
//判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
$(this).keyup(function(){
var val=$(this).val();
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
})
})
})
</script>
<style type="text/css">
form{width:400px;margin:10px auto;border:solid 1px #E0DEDE;background:#FCF9EF;padding:30px;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;}
label{display:block;height:40px;position:relative;margin:20px 0;}
span{position:absolute;float:left;line-height:40px;left:10px;color:#BCBCBC;cursor:text;}
.input_txt{width:398px;border:solid 1px #ccc;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;height:38px;text-indent:10px;}
.input_txt:focus{box-shadow:0 0 4px rgba(255,153,164,0.8);border:solid 1px #B00000;}
.border_radius{border-radius:5px;color:#B00000;}
h2{font-family:"微軟雅黑";text-shadow:1px 1px 3px #fff;}
</style>
</head>
<body>
<form class="border_radius" id="focus">
<h2>聚焦型提示語消失</h2>
<label><span>花瓣注冊(cè)郵箱</span><input type="text" class="input_txt border_radius" /></label>
<label><span>密碼</span><input type="text" class="input_txt border_radius" /></label>
</form>
<form class="border_radius" id="keydown">
<h2>輸入型提示語消失</h2>
<label><span>花瓣注冊(cè)郵箱</span><input type="text" class="input_txt border_radius" /></label>
<label><span>密碼</span><input type="text" class="input_txt border_radius" /></label>
</form>
</body>
</html>
基于HTML5 input標(biāo)簽的新特性 - placeholder 。另外,x-webkit-speech 屬性可以實(shí)現(xiàn)語音輸入功能。
復(fù)制代碼 代碼如下:
<div><input type="email" name="email" spellcheck="false" placeholder="郵 箱" autofocus tabindex="1" x-webkit-speech></div>
<div><input type="password" name="password" placeholder="密 碼" tabindex="2"></div>
第二種方法:
用span模擬,定位span,借助JS鍵盤事件判斷輸入,確定span里的內(nèi)容顯示隱藏。
復(fù)制代碼 代碼如下:
<!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="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#focus .input_txt").each(function(){
var thisVal=$(this).val();
//判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
//聚焦型輸入框驗(yàn)證
$(this).focus(function(){
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
});
})
$("#keydown .input_txt").each(function(){
var thisVal=$(this).val();
//判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
$(this).keyup(function(){
var val=$(this).val();
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
})
})
})
</script>
<style type="text/css">
form{width:400px;margin:10px auto;border:solid 1px #E0DEDE;background:#FCF9EF;padding:30px;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;}
label{display:block;height:40px;position:relative;margin:20px 0;}
span{position:absolute;float:left;line-height:40px;left:10px;color:#BCBCBC;cursor:text;}
.input_txt{width:398px;border:solid 1px #ccc;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;height:38px;text-indent:10px;}
.input_txt:focus{box-shadow:0 0 4px rgba(255,153,164,0.8);border:solid 1px #B00000;}
.border_radius{border-radius:5px;color:#B00000;}
h2{font-family:"微軟雅黑";text-shadow:1px 1px 3px #fff;}
</style>
</head>
<body>
<form class="border_radius" id="focus">
<h2>聚焦型提示語消失</h2>
<label><span>花瓣注冊(cè)郵箱</span><input type="text" class="input_txt border_radius" /></label>
<label><span>密碼</span><input type="text" class="input_txt border_radius" /></label>
</form>
<form class="border_radius" id="keydown">
<h2>輸入型提示語消失</h2>
<label><span>花瓣注冊(cè)郵箱</span><input type="text" class="input_txt border_radius" /></label>
<label><span>密碼</span><input type="text" class="input_txt border_radius" /></label>
</form>
</body>
</html>
您可能感興趣的文章:
- Flex 文本框的輸入下拉提示
- 頁(yè)面版文本框智能提示JS代碼
- asp.net 頁(yè)面版文本框智能提示JSCode (升級(jí)版)
- javascript 輸入文本框時(shí)的友好提示
- JavaScript實(shí)現(xiàn)帶自動(dòng)提示的文本框效果代碼
- JavaScript 文本框下拉提示(自動(dòng)提示)
- 簡(jiǎn)單JS自動(dòng)提示文本框代碼
- 基于JQuery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊文本框顯示隱藏提示文本
- 文本框輸入時(shí) 實(shí)現(xiàn)自動(dòng)提示(像百度、google一樣)
- JavaScript如何實(shí)現(xiàn)在文本框(密碼框)輸入提示語
相關(guān)文章
JS統(tǒng)計(jì)Flash被網(wǎng)友點(diǎn)擊過的代碼
JS統(tǒng)計(jì)Flash被網(wǎng)友點(diǎn)擊過的代碼...2007-05-05十個(gè)你必須要會(huì)的TypeScript技巧分享
學(xué)習(xí)Typescript通常是一個(gè)重新發(fā)現(xiàn)的過程。這篇文章為大家整理了十個(gè)你必須要會(huì)的TypeScript技巧,希望對(duì)大家學(xué)習(xí)TypeScript有所幫助2023-06-06JavaScript?數(shù)據(jù)結(jié)構(gòu)之字典方法
這篇文章主要介紹了JavaScript?數(shù)據(jù)結(jié)構(gòu)之字典方法,字典即Dictionary,是一種以?鍵-值對(duì)?形式存儲(chǔ)數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu),下文更多相關(guān)介紹需要的小伙伴可以參考一下2022-04-04HTML中用JS實(shí)現(xiàn)旋轉(zhuǎn)的圣誕樹
這篇文章介紹了HTML中用JS實(shí)現(xiàn)旋轉(zhuǎn)的圣誕樹,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12微信小程序云開發(fā)實(shí)現(xiàn)云數(shù)據(jù)庫(kù)讀寫權(quán)限
這篇文章主要為大家詳細(xì)介紹了微信小程序云開發(fā)實(shí)現(xiàn)云數(shù)據(jù)庫(kù)讀寫權(quán)限,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

JavaScript中重名的函數(shù)與對(duì)象示例詳析
最近同事問了一個(gè)問題,說在js中如果函數(shù)與對(duì)象重名了會(huì)怎么樣?仔細(xì)詳細(xì)這個(gè)問題值得討論一下,所以便有了這篇文章,這篇文章主要給大家介紹了關(guān)于JavaScript中重名的函數(shù)與對(duì)象的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧啊。
2017-09-09 
將函數(shù)的實(shí)際參數(shù)轉(zhuǎn)換成數(shù)組的方法
實(shí)際參數(shù)在函數(shù)中我們可以使用 arguments 對(duì)象獲得 (注:形參可通過 arguments.callee 獲得),雖然 arguments 對(duì)象與數(shù)組形似,但仍不是真正意義上的數(shù)組。
2010-01-01