ie9 placeholder兼容代码方法

时间:2024-09-23 20:07:44
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
} function placeholderIe() {
function isPlaceholder() {
var input = document.createElement('input');
return 'placeholder' in input;
}
if (!isPlaceholder()) { //不支持placeholder 用jquery来完成
$(document).ready(function() {
if (!isPlaceholder()) {
$("input,textarea").each(
function() {
if ($(this).val() == "" && $(this).attr("placeholder") != null) {
var self = this;
var onlyId = guid();
$(self).attr("data-pid", onlyId) function gen() {
var x = $(self)[0].offsetLeft;
var y = $(self)[0].offsetTop;
var h = $(self).height();
var left = parseInt(x) + 10;
var html = '<span id="' + onlyId + '" class="holder-span" style="left:' + left + 'px;top:' + y + 'px;line-height:' + h + 'px">' + $(self).attr("placeholder") + '</span>';
$(self).parent().append($(html))
}
gen()
$(this).focus(function() {
$("#" + onlyId + "").hide()
});
$(this).blur(function() {
if ($(this).val() == "" && $(this).attr("placeholder") != "") {
$("#" + onlyId + "").show()
}
});
$("#" + onlyId + "").click(function() {
$(this).hide()
if ($(self).attr("data-pid") == onlyId) {
$(self).focus()
}
});
}
});
}
});
}
}