js+css實(shí)現(xiàn)增加表單可用性之提示文字
更新時(shí)間:2013年06月03日 14:53:37 作者:
平常設(shè)計(jì)表單的時(shí)候,我們會(huì)加入一些提示文字,最常見的做法是利用value來設(shè)置,下面與大家分享一個(gè)實(shí)例,感興趣的朋友可以參考下哈
平常設(shè)計(jì)表單的時(shí)候,我們會(huì)加入一些提示文字,比如說在搜索框里,我們會(huì)提示“請輸入關(guān)鍵字”,并在搜索框得到焦點(diǎn)和失去焦點(diǎn)的時(shí)候適時(shí)的隱藏和顯示,最常見的做法是利用value來設(shè)置:
<form id="search">
<input type="text" id="keyword" name="keyword" value="請輸入關(guān)鍵字">
<button>搜索</button>
</form>
<script>
document.getElementById("keyword").onfocus = function() {
if (document.getElementById("keyword").value == "請輸入關(guān)鍵字") {
document.getElementById("keyword").value = "";
}
}
document.getElementById("keyword").onblur = function() {
if (document.getElementById("keyword").value == "") {
document.getElementById("keyword").value = "請輸入關(guān)鍵字";
}
}
document.getElementById("search").onsubmit = function() {
var keyword = document.getElementById("keyword").value;
if (keyword == "" || keyword == "請輸入關(guān)鍵字") {
alert("請輸入關(guān)鍵字");
return false;
}
return true;
}
</script>
如此的代碼雖然實(shí)現(xiàn)了我們要的功能,但卻不干凈,原因在于“請輸入關(guān)鍵字”這樣的文本僅僅是提示文字而已,而不是value,雖然技術(shù)上沒有大問題,但很多時(shí)候還是顯得麻煩,比如說我們可能像讓提示文字顯示的顏色是灰色,而用戶鍵入的文本則顯示黑色。
下面看看如何利用css來實(shí)現(xiàn)更好的方式:
<style>
#wrapper { position: relative; display: inline; }
#description { position: absolute; left: 1px; color: #999999; display: none; }
</style>
<form id="search">
<div id="wrapper">
<label for="keyword" id="description">請輸入關(guān)鍵字</label>
<input type="text" id="keyword" name="keyword">
</div>
<button>搜索</button>
</form>
<script>
window.onload = function() {
if (!document.getElementById("keyword").value) {
document.getElementById("description").style.display = "inline";
}
};
document.getElementById("keyword").onfocus = function() {
if (!document.getElementById("keyword").value) {
document.getElementById("description").style.display = "none";
}
}
document.getElementById("keyword").onblur = function() {
if (!document.getElementById("keyword").value) {
document.getElementById("description").style.display = "inline";
}
}
document.getElementById("search").onsubmit = function() {
if (!document.getElementById("keyword").value) {
alert("請輸入關(guān)鍵字");
return false;
}
return true;
}
</script>
這樣的實(shí)現(xiàn)方式雖然CSS,JS代碼都多了一些,但是結(jié)構(gòu)更合理,通過引入label來顯示提示文字(通過CSS的position屬性定位),讓value本身更單純,而且提示文字和用戶輸入的文本在樣式更容易控制,比如顏色的深淺,從而提高表單可用性。
復(fù)制代碼 代碼如下:
<form id="search">
<input type="text" id="keyword" name="keyword" value="請輸入關(guān)鍵字">
<button>搜索</button>
</form>
<script>
document.getElementById("keyword").onfocus = function() {
if (document.getElementById("keyword").value == "請輸入關(guān)鍵字") {
document.getElementById("keyword").value = "";
}
}
document.getElementById("keyword").onblur = function() {
if (document.getElementById("keyword").value == "") {
document.getElementById("keyword").value = "請輸入關(guān)鍵字";
}
}
document.getElementById("search").onsubmit = function() {
var keyword = document.getElementById("keyword").value;
if (keyword == "" || keyword == "請輸入關(guān)鍵字") {
alert("請輸入關(guān)鍵字");
return false;
}
return true;
}
</script>
如此的代碼雖然實(shí)現(xiàn)了我們要的功能,但卻不干凈,原因在于“請輸入關(guān)鍵字”這樣的文本僅僅是提示文字而已,而不是value,雖然技術(shù)上沒有大問題,但很多時(shí)候還是顯得麻煩,比如說我們可能像讓提示文字顯示的顏色是灰色,而用戶鍵入的文本則顯示黑色。
下面看看如何利用css來實(shí)現(xiàn)更好的方式:
復(fù)制代碼 代碼如下:
<style>
#wrapper { position: relative; display: inline; }
#description { position: absolute; left: 1px; color: #999999; display: none; }
</style>
<form id="search">
<div id="wrapper">
<label for="keyword" id="description">請輸入關(guān)鍵字</label>
<input type="text" id="keyword" name="keyword">
</div>
<button>搜索</button>
</form>
<script>
window.onload = function() {
if (!document.getElementById("keyword").value) {
document.getElementById("description").style.display = "inline";
}
};
document.getElementById("keyword").onfocus = function() {
if (!document.getElementById("keyword").value) {
document.getElementById("description").style.display = "none";
}
}
document.getElementById("keyword").onblur = function() {
if (!document.getElementById("keyword").value) {
document.getElementById("description").style.display = "inline";
}
}
document.getElementById("search").onsubmit = function() {
if (!document.getElementById("keyword").value) {
alert("請輸入關(guān)鍵字");
return false;
}
return true;
}
</script>
這樣的實(shí)現(xiàn)方式雖然CSS,JS代碼都多了一些,但是結(jié)構(gòu)更合理,通過引入label來顯示提示文字(通過CSS的position屬性定位),讓value本身更單純,而且提示文字和用戶輸入的文本在樣式更容易控制,比如顏色的深淺,從而提高表單可用性。
您可能感興趣的文章:
- 表單JS彈出填寫提示效果代碼
- 實(shí)用的JS表單驗(yàn)證提示效果
- javascript寫的一個(gè)表單動(dòng)態(tài)輸入提示的代碼
- js下在password表單內(nèi)顯示提示信息的解決辦法
- js實(shí)現(xiàn)表單檢測及表單提示的方法
- JavaScript DOM學(xué)習(xí)第八章 表單錯(cuò)誤提示
- javascript中IE瀏覽器不支持NEW DATE()帶參數(shù)的解決方法
- IE8的JavaScript點(diǎn)擊事件(onclick)不兼容的解決方法
- 讓ie運(yùn)行js時(shí)提示允許阻止內(nèi)容運(yùn)行的解決方法
- Javascript在IE下設(shè)置innerHTML時(shí)出現(xiàn)未知的運(yùn)行時(shí)錯(cuò)誤的解決方法
- JavaScript的常見兼容問題及相關(guān)解決方法(chrome/IE/firefox)
- IE瀏覽器下JS腳本提交表單后,不能自動(dòng)提示問題解決方法
相關(guān)文章
js實(shí)現(xiàn)點(diǎn)擊鏈接后延遲3秒再跳轉(zhuǎn)的方法
這篇文章主要介紹了js實(shí)現(xiàn)點(diǎn)擊鏈接后延遲3秒再跳轉(zhuǎn)的方法,通過javascript的setTimeout方法實(shí)現(xiàn)延遲跳轉(zhuǎn)的功能,需要的朋友可以參考下2015-06-06js實(shí)現(xiàn)單層數(shù)組轉(zhuǎn)多層樹
這篇文章主要介紹了js實(shí)現(xiàn)單層數(shù)組轉(zhuǎn)多層樹方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06JavaScript中???、??=、?.和?||的區(qū)別淺析
在?JS?中,???運(yùn)算符被稱為非空運(yùn)算符,下面這篇文章主要給大家介紹了關(guān)于JavaScript中???、??=、?.和?||區(qū)別的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08js隨機(jī)顏色代碼的多種實(shí)現(xiàn)方式
本篇文章小編為大家介紹,js隨機(jī)顏色代碼的多種實(shí)現(xiàn)方式。需要的朋友參考下2013-04-04js+html5實(shí)現(xiàn)手機(jī)九宮格密碼解鎖功能
這篇文章主要為大家詳細(xì)介紹了js+html5實(shí)現(xiàn)手機(jī)九宮格密碼解鎖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07JavaScript 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)元素實(shí)例代碼
這篇文章主要介紹了JavaScript 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)元素實(shí)例代碼,需要的朋友可以參考下2014-02-02