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

具體代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery實現(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);
}
});
//獲取焦點時設置內(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('');
}
});
//失去焦點后還原提示內(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="請輸入賬戶" /><br><br>
<input type="text" class="holder" name="name" value="" data-holder="請輸入密碼" />
</body>
</html>
更多關于jQuery相關內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見經(jīng)典特效匯總》、《jQuery常用插件及用法總結》、《jQuery表格(table)操作技巧匯總》、《jQuery form操作技巧匯總》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴展技巧總結》、《jQuery拖拽特效與技巧總結》、《jquery中Ajax用法總結》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
- js兼容的placeholder屬性詳解
- js模仿html5 placeholder適應于不支持的瀏覽器
- javascript 文本框水印/占位符(watermark/placeholder)實現(xiàn)方法
- 讓低版本瀏覽器支持input的placeholder屬性(js方法)
- 兩種方法基于jQuery實現(xiàn)IE瀏覽器兼容placeholder效果
- 使用jQuery快速解決input中placeholder值在ie中無法支持的問題
- IE下支持文本框和密碼框placeholder效果的JQuery插件分享
- 基于jQuery的讓非HTML5瀏覽器支持placeholder屬性的代碼
- jQuery實現(xiàn)的一個自定義Placeholder屬性插件
- jQuery插件EnPlaceholder實現(xiàn)輸入框提示文字
- 基于JS實現(xiàn)html中placeholder屬性提示文字效果示例
相關文章
擴展jquery實現(xiàn)客戶端表格的分頁、排序功能代碼
下面鏈接中是我用jQuery的擴展來實現(xiàn)的表格分頁和排序,使用這個擴展必須加上表頭<thead>和<tbody>標簽,因為我是 通過<tbody>來進行分頁的,要是不加thead,那么表頭也要作為分頁計算時的一個行了。2011-03-03
jQuery 可以拖動的div實現(xiàn)代碼 腳本之家修正版
最近研究了一下jQuery,覺得真的是一個很不錯的js庫,其他的不說,關鍵是有翔實的文檔,這點是非常關鍵的。2009-06-06
jQuery實現(xiàn)仿QQ頭像閃爍效果的文字閃動提示代碼
這篇文章主要介紹了jQuery實現(xiàn)仿QQ頭像閃爍效果的文字閃動提示代碼,涉及jQuery正則表達式及定時函數(shù)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11

