ie placeholder屬性的兼容性問題解決方法
發(fā)布時(shí)間:2014-06-30 17:56:19 作者:佚名
我要評(píng)論

html 5有個(gè)很棒的屬性,placeholder,但是在不支持html5的低版本的瀏覽器中,因此要人為的去實(shí)現(xiàn)placeholder屬性,下面有個(gè)示例,感興趣的朋友可以參考下
html 5 有個(gè)很棒的屬性,placeholder,在鼠標(biāo)聚焦到上面時(shí)候,提示文字會(huì)消失,失去焦點(diǎn)之后,又會(huì)出現(xiàn):
但是在不支持html5的低版本的瀏覽器中,placeholder屬性是無效的,為了解決這個(gè)問題,因此,人為的去實(shí)現(xiàn)placeholder屬性:
//placeholder功能實(shí)現(xiàn)
var placeholder = {
add: function (el) {
if (!('placeholder' in document.createElement('input'))) {
var self = placeholder;
el.each(function (e) {
if (IsEmpty(e.value()) || e.value() == e.attr('placeholder')) {
e.value(e.attr('placeholder'));
e.css('color', 'gray');
}
else {
e.css('color', 'black');
}
});
el.bind('focus', self._onfocus);
el.bind('click', self._onfocus);
el.bind('blur', self._onblur);
el.bind('keyup', self._onkeyup);
}
},
remove: function (el) {
if (!('placeholder' in document.createElement('input'))) {
var self = placeholder;
el.unbind('focus', self._onfocus);
el.unbind('click', self._onfocus);
el.unbind('blur', self._onblur);
}
},
check: function (el) {
if (!('placeholder' in document.createElement('input'))) {
el.each(function (tar) {
if (IsEmpty(tar.value())) {
tar.value(tar.attr('placeholder'));
}
});
}
},
clear: function () {
if (!('placeholder' in document.createElement('input'))) {
$('input[type="text"]').each(function (el) {
if (el.value() == el.attr('placeholder')) {
el.value('');
}
});
$('textarea').each(function (el) {
if (el.value() == el.attr('placeholder')) {
el.value('');
}
});
}
},
_onfocus: function () {
if ($(this).value() == $(this).attr('placeholder'))
$(this).value('');
},
_onblur: function () {
if (IsEmpty($(this).value()) || $(this).value() == $(this).attr('placeholder')) {
$(this).value($(this).attr('placeholder'));
$(this).css('color', 'gray');
}
else {
$(this).css('color', 'black');
}
},
_onkeyup: function () {
if (IsEmpty($(this).value())) {
$(this).css('color', 'gray');
}
else {
$(this).css('color', 'black');
}
}
};
使用時(shí)候:
placeholder.add($('input[type="text"]'));
placeholder.add($('textarea'));
需要注意的是,考慮到如果input的type是password的時(shí)候,placeholder顯示的是.....的屬性
這種情況下,解決方法為:
給定兩個(gè)輸入框,
一個(gè)是text,一個(gè)為password的,
在有焦點(diǎn)的時(shí)候,切換為password,失去焦點(diǎn)的時(shí)候,切換為text用來展示placeholder屬性.
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
var pwd = $("#pwd");
var password = $("#password");
pwd.focus(function(){
pwd.hide();
password.show().focus();
});
password.focusout(function(){
if(password.val().trim() === ""){
password.hide();
pwd.show();
}
});
});
</script>
<input type="text" id="pwd" value="請(qǐng)輸入密碼"/>
<input type="password" id="password" style="display:none;"/>
但是在不支持html5的低版本的瀏覽器中,placeholder屬性是無效的,為了解決這個(gè)問題,因此,人為的去實(shí)現(xiàn)placeholder屬性:
復(fù)制代碼
代碼如下://placeholder功能實(shí)現(xiàn)
var placeholder = {
add: function (el) {
if (!('placeholder' in document.createElement('input'))) {
var self = placeholder;
el.each(function (e) {
if (IsEmpty(e.value()) || e.value() == e.attr('placeholder')) {
e.value(e.attr('placeholder'));
e.css('color', 'gray');
}
else {
e.css('color', 'black');
}
});
el.bind('focus', self._onfocus);
el.bind('click', self._onfocus);
el.bind('blur', self._onblur);
el.bind('keyup', self._onkeyup);
}
},
remove: function (el) {
if (!('placeholder' in document.createElement('input'))) {
var self = placeholder;
el.unbind('focus', self._onfocus);
el.unbind('click', self._onfocus);
el.unbind('blur', self._onblur);
}
},
check: function (el) {
if (!('placeholder' in document.createElement('input'))) {
el.each(function (tar) {
if (IsEmpty(tar.value())) {
tar.value(tar.attr('placeholder'));
}
});
}
},
clear: function () {
if (!('placeholder' in document.createElement('input'))) {
$('input[type="text"]').each(function (el) {
if (el.value() == el.attr('placeholder')) {
el.value('');
}
});
$('textarea').each(function (el) {
if (el.value() == el.attr('placeholder')) {
el.value('');
}
});
}
},
_onfocus: function () {
if ($(this).value() == $(this).attr('placeholder'))
$(this).value('');
},
_onblur: function () {
if (IsEmpty($(this).value()) || $(this).value() == $(this).attr('placeholder')) {
$(this).value($(this).attr('placeholder'));
$(this).css('color', 'gray');
}
else {
$(this).css('color', 'black');
}
},
_onkeyup: function () {
if (IsEmpty($(this).value())) {
$(this).css('color', 'gray');
}
else {
$(this).css('color', 'black');
}
}
};
使用時(shí)候:
復(fù)制代碼
代碼如下:placeholder.add($('input[type="text"]'));
placeholder.add($('textarea'));
需要注意的是,考慮到如果input的type是password的時(shí)候,placeholder顯示的是.....的屬性
這種情況下,解決方法為:
給定兩個(gè)輸入框,
一個(gè)是text,一個(gè)為password的,
在有焦點(diǎn)的時(shí)候,切換為password,失去焦點(diǎn)的時(shí)候,切換為text用來展示placeholder屬性.
復(fù)制代碼
代碼如下:<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
var pwd = $("#pwd");
var password = $("#password");
pwd.focus(function(){
pwd.hide();
password.show().focus();
});
password.focusout(function(){
if(password.val().trim() === ""){
password.hide();
pwd.show();
}
});
});
</script>
<input type="text" id="pwd" value="請(qǐng)輸入密碼"/>
<input type="password" id="password" style="display:none;"/>
相關(guān)文章
- 這篇文章主要為大家詳細(xì)介紹了HTML5 placeholder屬性,placeholder屬性能夠讓你在文本框里顯示提示信息,感興趣的小伙伴們可以參考一下2016-06-22
- Placeholder是HTML5新增的另一個(gè)屬性,當(dāng)input或者textarea設(shè)置了該屬性后,該值的內(nèi)容將作為灰字提示顯示在文本框中,當(dāng)文本框獲得焦點(diǎn)時(shí),提示文字消失2014-09-02
HTML 5 input placeholder 屬性如何完美兼任ie
這篇文章主要介紹了HTML 5 input placeholder 屬性完美兼任ie的方法,需要的朋友可以參考下2014-05-12- 瀏覽器引入了許多的HTML5 特性其中我最喜歡的一個(gè)就是為input元素引入了placeholder屬性,placeholder屬性顯示引導(dǎo)性文字直到輸入框獲取輸入焦點(diǎn),當(dāng)有了用戶輸入內(nèi)容后引導(dǎo)2013-08-07
限制html文本框input只能輸入數(shù)字和小數(shù)點(diǎn)
本文主要介紹了限制html文本框input只能輸入數(shù)字和小數(shù)點(diǎn)的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-27使一個(gè)input文本框隨其中內(nèi)容而變化長(zhǎng)度的方法
這篇文章主要介紹了如何使一個(gè)input文本框隨其中內(nèi)容長(zhǎng)度變化而變化長(zhǎng)度,實(shí)現(xiàn)原理很簡(jiǎn)單,大家看過之后就知道了2014-04-08使用placeholder屬性設(shè)置input文本框的提示信息
這篇文章主要介紹了使用placeholder屬性設(shè)置input文本框的提示信息,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-19