IE10-浏览器实现placeholder效果
2015-09-03 11:57
375 查看
如下图,在文本框为空时显示提示文字
在IE10+和chrome浏览器加placeholder属性及可实现 ,单在IE10-浏览器并不支持该属性,
以下是placeholder在IE10-浏览器的实现
html:
在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="请输入用户名" />
相关文章推荐
- 百钱百鸡
- 将同一用户的某一字段的所有内容拼接在一起
- 【HDU1083】【最大二分匹配】
- SEO生命周期,别做死了都不知道!
- 设计模式之依赖倒置原则
- Win10 Mobile越狱解锁部署:一款替换字体的应用程序出现
- 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路
- Bash 里的转义序列
- 设计模式之:适配器模式
- 公有ip地址 和内网ip地址
- 将相机/相册的图片保存到本地沙盒
- C# Excel 为图表添加趋势线、误差线
- 【android】 如何把gif图片下载到本地
- Regionals 2014 >> Latin America
- JS-案例-节点交换
- poj 1719最大二分匹配hungary算法
- 设置你的开发环境(Eclipse中,IntelliJ IDEA的,NetBeans的)
- 下个Win10 Mobile预览版将修复10512系统众多Bug 包括网络共享
- 用图片数字显示对应的数目
- BZOJ 1263: [SCOI2006]整数划分( 高精度 )