您的位置:首页 > 其它

IE10-浏览器实现placeholder效果

2015-09-03 11:57 375 查看
如下图,在文本框为空时显示提示文字



在IE10+和chrome浏览器加placeholder属性及可实现 ,单在IE10-浏览器并不支持该属性,

以下是placeholder在IE10-浏览器的实现

<style type="text/css">
/*输入框为空时提示文字的样式*/
label.placeholder
{
color: gray;
padding-left: 3px;
cursor: text;
z-index: 1000;
position: absolute;
background: transparent;
font-size: 0.8em;
padding-top: 4px;
}
</style>
<script type="text/javascript">
/* 设置输入框为空时输入框内显示/隐藏提示文字
* @param show 是否显示提示文字,默认显示
*/
$.fn.setHint = function (show) {
if ('placeholder' in document.createElement('input'))
return;

var word = this.attr("placeholder");
if (word) {
show = (show == undefined) ? (this.val() == "") : show; //根据内容是否为空确定是否显示
if (show && this.val() == "") {
this.prev("label.placeholder").show();
} else if (!show) {
this.prev("label.placeholder").hide();
}
}
};

// 页面初始化执行的脚本
$(function () {
// IE10及以上浏览器支持placeholder属性,不需要用脚本实现
if (!('placeholder' in document.createElement('input'))) {
$(":text[placeholder],:password[placeholder],textarea[placeholder]").wrap("<span></span>")
.focus(function () {
$(this).prev("label.placeholder").hide();
}).blur(function () {
if ($(this).val() == "") {
$(this).prev("label.placeholder").show();
}
}).each(function () {
var labelHtml = "<label class='placeholder'>" + $(this).attr("placeholder") + "</label>";
$(labelHtml).insertBefore(this).click(function () {
$(this).hide().next().focus();
}).toggle($(this).val() == "");
});
}
});
</script>


html:

<input type="text" placeholder="请输入用户名" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: