手机
当前位置:查字典教程网 >网页设计 > Div+Css教程 >ie placeholder属性的兼容性问题解决方法
ie placeholder属性的兼容性问题解决方法
摘要:html5有个很棒的属性,placeholder,在鼠标聚焦到上面时候,提示文字会消失,失去焦点之后,又会出现:但是在不支持html5的低版...

 html 5 有个很棒的属性,placeholder,在鼠标聚焦到上面时候,提示文字会消失,失去焦点之后,又会出现:

但是在不支持html5的低版本的浏览器中,placeholder属性是无效的,为了解决这个问题,因此,人为的去实现placeholder属性:

代码如下:
//placeholder功能实现
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');
}
}
};

使用时候:

代码如下:
placeholder.add($('input[type="text"]'));
placeholder.add($('textarea'));

需要注意的是,考虑到如果input的type是password的时候,placeholder显示的是.....的属性

这种情况下,解决方法为:

给定两个输入框,

一个是text,一个为password的,

在有焦点的时候,切换为password,失去焦点的时候,切换为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="请输入密码"/>
<input type="password" id="password" style="display:none;"/>

【ie placeholder属性的兼容性问题解决方法】相关文章:

网页中表单按回车就自动提交的问题的解决方案

margin 负值引起的层级(z-index)问题

CSS div居中的三种方法

DIV下图片自适应解决方法

火狐在用offsetHeight获取div的高度时为0的解决方法

CSS之overflow属性用法

css margin属性兼容性分析

WEB标准布局的Div + CSS 高度自适应解决方法

DIV下图片自适应解决方法

添加css样式的三种方法

精品推荐
分类导航