placeholder(HTML 5) IE 兼容插件

本文介绍了一个用于在Internet Explorer中实现HTML5 placeholder属性的jQuery插件,该插件适用于IE9及以下版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

placeholder 这个属性被越来越频繁的使用.
但为做HTML 5 特性IE没能实现这东西.
以下的jQuery插件就是用来在IE上实现该属性的.
/**
 * [placeholder(HTML 5) IE 实现.IE9以下通过测试.]
 * v 1.0 by oTwo 2014年7月31日 11:45:29
 */
$.fn.placeholder = function() {
	// 当系统支持 placeholder 时使用系统自带的.
	if ('placeholder' in document.createElement('input')) {
		return;
	}
	this.each(function(index, val) {
		var ele = $(this);

		//创建 显示层
		var div = $('<div>').css({
			"color": "#cccccc",
			"cursor": "text",
			"position": "absolute",
			"display": ele.val() ? 'none' : 'block',
			"zIndex": ele.css('zIndex') == 'auto' ? 'auto' : ele.css('zIndex') - 0 + 1,
			"left": ele.offset().left + 14,
			"top": ele.offset().top,
			"width": ele.outerWidth() - 14,
			"height": ele.outerHeight()
		}).text(ele.attr('placeholder')).appendTo('body');

		//绑定事件
		div.click(function(e) {
			ele.focusin().focus();
		});
		ele.focusin(function() {
			if ($(this).val()) {
				return;
			}
			div.hide();
		});
		ele.focusout(function() {
			if ($(this).val()) {
				return;
			}
			div.show();
		});
	});
	return this;
};

 

IE(包括IE6)支持HTML5元素,我们需要在HTML头部添加以下JavaScript,这是一个简单的document.createElement声明,利用条件注释针对IE来调用这个js文件。Opera,FireFox等其他非IE浏览器就会忽视这段代码,也不会存在http请求。 <!–[if IE]> <script src=”http://html5shiv.googlecode.com/svn/trunk/html5.js”></script> <![endif]–> 上面这段代码仅会在IE浏览器下运行,还有一点需要注意,在页面中调用html5.js文件必须添加在页面的head元素内,因为IE浏览器必须在元素解析前知道这个元素,所以这个js文件不能在页面底部调用。 这个html5的js文件是作者把他放在Google code project上提供给大家可以直接调用的,当然,如果觉得这样会影响你的网页打开速度,你可以把html5的js文件直接下载下来让后上传到自己的服务器单独调用。 以下是html5的js文件中的代码: (function(){if(!/*@cc_on!@*/0)return;var e = “abbr,article,aside,audio,canvas,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video”.split(’,'),i=e.length;while(i–){document.createElement(e[i])}})() 除了在网页中调用包含以上代码的js文件来让IE浏览器支持HTML5元素以外,你也可以以下面这种方式把代码直接添加到网页中。 <!–[if IE]> <script> (function(){if(!/*@cc_on!@*/0)return;var e = “abbr,article,aside,audio,canvas,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video”.split(’,'),i=e.length;while(i–){document.createElement(e[i])}})() </script> <![endif]–>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值