js+css實(shí)現(xiàn)增加表單可用性之提示文字
更新時間:2013年06月03日 14:53:37 作者:
平常設(shè)計表單的時候,我們會加入一些提示文字,最常見的做法是利用value來設(shè)置,下面與大家分享一個實(shí)例,感興趣的朋友可以參考下哈
平常設(shè)計表單的時候,我們會加入一些提示文字,比如說在搜索框里,我們會提示“請輸入關(guān)鍵字”,并在搜索框得到焦點(diǎn)和失去焦點(diǎn)的時候適時的隱藏和顯示,最常見的做法是利用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ù)上沒有大問題,但很多時候還是顯得麻煩,比如說我們可能像讓提示文字顯示的顏色是灰色,而用戶鍵入的文本則顯示黑色。
下面看看如何利用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ù)上沒有大問題,但很多時候還是顯得麻煩,比如說我們可能像讓提示文字顯示的顏色是灰色,而用戶鍵入的文本則顯示黑色。
下面看看如何利用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寫的一個表單動態(tài)輸入提示的代碼
- js下在password表單內(nèi)顯示提示信息的解決辦法
- js實(shí)現(xiàn)表單檢測及表單提示的方法
- JavaScript DOM學(xué)習(xí)第八章 表單錯誤提示
- javascript中IE瀏覽器不支持NEW DATE()帶參數(shù)的解決方法
- IE8的JavaScript點(diǎn)擊事件(onclick)不兼容的解決方法
- 讓ie運(yùn)行js時提示允許阻止內(nèi)容運(yùn)行的解決方法
- Javascript在IE下設(shè)置innerHTML時出現(xiàn)未知的運(yùn)行時錯誤的解決方法
- JavaScript的常見兼容問題及相關(guān)解決方法(chrome/IE/firefox)
- IE瀏覽器下JS腳本提交表單后,不能自動提示問題解決方法
相關(guān)文章
js es6系列教程 - 基于new.target屬性與es5改造es6的類語法
下面小編就為大家?guī)硪黄猨s es6系列教程 - 基于new.target屬性與es5改造es6的類語法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
微信小程序scroll-view組件實(shí)現(xiàn)滾動動畫
這篇文章主要為大家詳細(xì)介紹了微信小程序scroll-view組件實(shí)現(xiàn)滾動動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Pixi.js實(shí)現(xiàn)可視化圖形編輯器的方法
本文主要介紹了Pixi.js實(shí)現(xiàn)可視化圖形編輯器的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
JavaScript String(字符串)對象的簡單實(shí)例(推薦)
下面小編就為大家?guī)硪黄狫avaScript String(字符串)對象的簡單實(shí)例(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
Html5 js實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了Html5 js實(shí)現(xiàn)手風(fēng)琴效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
JavaScript中輸出信息的方法(信息確認(rèn)框-提示輸入框-文檔流輸出)
這篇文章主要介紹了JavaScript中輸出信息的方法(信息確認(rèn)框-提示輸入框-文檔流輸出)的相關(guān)資料,需要的朋友可以參考下2016-06-06

