您现在的位置是:网站首页> 编程资料编程资料
ie placeholder属性的兼容性问题解决方法HTML5 placeholder属性详解让IE下支持Html5的placeholder属性的插件HTML 5 input placeholder 属性如何完美兼任ieHTML5 placeholder(空白提示)属性介绍限制html文本框input只能输入数字和小数点使一个input文本框随其中内容而变化长度的方法使用placeholder属性设置input文本框的提示信息
2023-10-25
273人已围观
简介 html 5有个很棒的属性,placeholder,但是在不支持html5的低版本的浏览器中,因此要人为的去实现placeholder属性,下面有个示例,感兴趣的朋友可以参考下
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属性.
但是在不支持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属性.
复制代码
代码如下:
相关内容
- a标签的css样式四个状态LVHA的设计CSS Transition通过改变Height实现展开收起元素从QQtabBar看css命名规范BEM的详细介绍css实现两栏布局,左侧固定宽,右侧自适应的多种方法CSS 实现Chrome标签栏的技巧CSS实现两列布局的N种方法CSS实现隐藏搜索框功能(动画正反向序列)CSS3中Animation实现简单的手指点击动画的示例详解CSS中的特指度和层叠问题详解overflow:hidden的作用(溢出隐藏、清除浮动、解决外边距塌陷)关于CSS浮动与取消浮动的问题
- 天天德州 拿到小牌怎么办 拿到小牌翻盘技巧_手机游戏_游戏攻略_
- 全民小镇 如何增加繁荣度 繁荣度系统详细介绍_手机游戏_游戏攻略_
- 雷霆战机散弹机关炮系统全面解析_手机游戏_游戏攻略_
- 雷霆战机超高速火炮系统全面解析_手机游戏_游戏攻略_
- 雷霆战机怒火凤凰系统全面解析_手机游戏_游戏攻略_
- 我叫MT3.6版全新卡牌老相好获得的方法_手机游戏_游戏攻略_
- 我叫MT黑龙实战视频赏析 魔法契约强大威力_手机游戏_游戏攻略_
- 我叫MT最大利益化花费符石送院长活动来袭_手机游戏_游戏攻略_
- 我叫MT购买符石即可获得紫卡血精灵及院长加丁_手机游戏_游戏攻略_
