本文實(shí)例講述了jQuery實(shí)現(xiàn)的placeholder效果。分享給大家供大家參考,具體如下:
運(yùn)行效果截圖如下:

具體代碼如下:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery實(shí)現(xiàn)placeholder效果</title> <script src="jquery-1.7.2.min.js"></script> <script> $(function () { initEvent(); }); //初始化提示內(nèi)容的顏色 function initEvent() { $('input.holder').each(function () { var $this = $(this), holder = $this.data('holder'); if (holder) { $this.css('color', '#a9a9a9').val(holder); } }); //獲取焦點(diǎn)時(shí)設(shè)置內(nèi)容的顏色和值為空 $(document).off('focus', 'input.holder').on('focus', 'input.holder', function () { var $this = $(this); if ($this.val() === $this.data('holder')) { $this.css('color', 'black').val(''); } }); //失去焦點(diǎn)后還原提示內(nèi)容 $(document).off('focusout', 'input.holder').on('focusout', 'input.holder', function () { var $this = $(this); if ($.trim($this.val()) === '') { $this.css('color', '#a9a9a9').val($this.data('holder')); } }); } </script></head><body> <input type="text" class="holder" name="name" value="" data-holder="請(qǐng)輸入賬戶" /><br><br> <input type="text" class="holder" name="name" value="" data-holder="請(qǐng)輸入密碼" /></body></html>更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery form操作技巧匯總》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注