IE下支持文本框和密碼框placeholder效果的JQuery插件分享
很久之前寫了這個插件,基于jQuery實(shí)現(xiàn)的,主要用于IE下實(shí)現(xiàn)placeholder效果,可同時支持文本和密碼輸入框。
placeholder是HTML5新增的一個屬性,當(dāng)input設(shè)置了該屬性后,該值的內(nèi)容將作為灰色提示顯示在文本框中,當(dāng)文本框獲得焦點(diǎn)時,提示文字消失。
下載地址:http://xiazai.jb51.net/201501/other/placeholderfriend.rar
實(shí)現(xiàn)代碼如下:
(function($) {
/**
* 沒有開花的樹
* 2012/11/28 15:12
*/
var placeholderfriend = {
focus: function(s) {
s = $(s).hide().prev().show().focus();
var idValue = s.attr("id");
if (idValue) {
s.attr("id", idValue.replace("placeholderfriend", ""));
}
var clsValue = s.attr("class");
if (clsValue) {
s.attr("class", clsValue.replace("placeholderfriend", ""));
}
}
}
//判斷是否支持placeholder
function isPlaceholer() {
var input = document.createElement('input');
return "placeholder" in input;
}
//不支持的代碼
if (!isPlaceholer()) {
$(function() {
var form = $(this);
//遍歷所有文本框,添加placeholder模擬事件
var elements = form.find("input[type='text'][placeholder]");
elements.each(function() {
var s = $(this);
var pValue = s.attr("placeholder");
var sValue = s.val();
if (pValue) {
if (sValue == '') {
s.val(pValue);
}
}
});
elements.focus(function() {
var s = $(this);
var pValue = s.attr("placeholder");
var sValue = s.val();
if (sValue && pValue) {
if (sValue == pValue) {
s.val('');
}
}
});
elements.blur(function() {
var s = $(this);
var pValue = s.attr("placeholder");
var sValue = s.val();
if (!sValue) {
s.val(pValue);
}
});
//遍歷所有密碼框,添加placeholder模擬事件
var elementsPass = form.find("input[type='password'][placeholder]");
elementsPass.each(function(i) {
var s = $(this);
var pValue = s.attr("placeholder");
var sValue = s.val();
if (pValue) {
if (sValue == '') {
//DOM不支持type的修改,需要復(fù)制密碼框?qū)傩?,生成新的DOM
var html = this.outerHTML || "";
html = html.replace(/\s*type=(['"])?password\1/gi, " type=text placeholderfriend")
.replace(/\s*(?:value|on[a-z]+|name)(=(['"])?\S*\1)?/gi, " ")
.replace(/\s*placeholderfriend/, " placeholderfriend value='" + pValue
+ "' " + "onfocus='placeholderfriendfocus(this);' ");
var idValue = s.attr("id");
if (idValue) {
s.attr("id", idValue + "placeholderfriend");
}
var clsValue = s.attr("class");
if (clsValue) {
s.attr("class", clsValue + "placeholderfriend");
}
s.hide();
s.after(html);
}
}
});
elementsPass.blur(function() {
var s = $(this);
var sValue = s.val();
if (sValue == '') {
var idValue = s.attr("id");
if (idValue) {
s.attr("id", idValue + "placeholderfriend");
}
var clsValue = s.attr("class");
if (clsValue) {
s.attr("class", clsValue + "placeholderfriend");
}
s.hide().next().show();
}
});
});
}
window.placeholderfriendfocus = placeholderfriend.focus;
})(jQuery);
使用很簡單,例子如下:
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="placeholderfriend.js" type="text/javascript"></script>
</head>
<body>
<input placeholder="賬號/手機(jī)號碼" ><br>
<input placeholder="密碼" type="password" >
</body>
</html>
- js兼容的placeholder屬性詳解
- js模仿html5 placeholder適應(yīng)于不支持的瀏覽器
- javascript 文本框水印/占位符(watermark/placeholder)實(shí)現(xiàn)方法
- 讓低版本瀏覽器支持input的placeholder屬性(js方法)
- 兩種方法基于jQuery實(shí)現(xiàn)IE瀏覽器兼容placeholder效果
- 使用jQuery快速解決input中placeholder值在ie中無法支持的問題
- 基于jQuery的讓非HTML5瀏覽器支持placeholder屬性的代碼
- jQuery實(shí)現(xiàn)的placeholder效果完整實(shí)例
- jQuery實(shí)現(xiàn)的一個自定義Placeholder屬性插件
- jQuery插件EnPlaceholder實(shí)現(xiàn)輸入框提示文字
- 基于JS實(shí)現(xiàn)html中placeholder屬性提示文字效果示例
相關(guān)文章
jquery使用FormData實(shí)現(xiàn)異步上傳文件
這篇文章主要為大家詳細(xì)介紹了jquery使用FormData實(shí)現(xiàn)異步上傳文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
jQuery簡單tab切換效果實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery簡單tab切換效果實(shí)現(xiàn)方法,涉及jQuery鏈?zhǔn)讲僮骷氨闅v的技巧,是非常具有實(shí)用價值的功能,需要的朋友可以參考下2015-04-04
jQuery LigerUI 插件介紹及使用之ligerDrag和ligerResizable示例代碼打包
jQuery LigerUI 插件介紹及使用之ligerDrag和ligerResizable使用。2011-04-04
jQuery獲取及設(shè)置表單input各種類型值的方法小結(jié)
這篇文章主要介紹了jQuery獲取及設(shè)置表單input各種類型值的方法,總結(jié)分析了jQuery針對表單元素的各種常見操作技巧,非常簡單實(shí)用,需要的朋友可以參考下2016-05-05
jquery獲取easyui日期控件的值實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨query獲取easyui日期控件的值實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
JQuery 插件制作實(shí)踐 xMarquee插件V1.0
今天要介紹的是實(shí)現(xiàn)類跑馬燈效果的的廣告插件。類似偶公司web-dev的同事在網(wǎng)站首頁上做的目錄廣告播放器。其實(shí)很簡單,LEVIN實(shí)際應(yīng)用中也用到,所以稍作整理如下.2010-04-04

