兼容IE8 input placeholder 兼容问题

333 阅读1分钟
/***************************************
 * name: placeholder
 * tips: 输入框默认提示-兼容IE9
****************************************/
function placeholderIe(){
	supportPlaceholder = 'placeholder' in document.createElement('input');
	placeholder = function (input) {
		var text = input.attr('placeholder');
		defaultValue = input.defaultValue;
		if (!defaultValue) {
			input.val(text).addClass("phcolor");
		}
		input.focus(function () {
			if (input.val() == text) {
				$(this).val("");
			}
		})
		input.blur(function () {
			if (input.val() == "") {
				$(this).val(text).addClass("phcolor");
			}
		});
		//输入的字符不为灰色
		input.keydown(function () {
			$(this).removeClass("phcolor");
		})
	}
	//当浏览器不支持placeholder属性时,调用placeholder函数
	if (!supportPlaceholder) {
		$('input').each(function () {
		    //当input中有值时,跳出本次赋值
			if($(this).val()!=''){return}
			text = $(this).attr("placeholder");
			if ($(this).attr("type") == "text") {
				placeholder($(this));
			}
		})
    )}
}