您的位置:首页 > 其它

PlaceHolder 兼容性解决方法

2016-07-26 16:18 204 查看
PlaceHolder 是HTML5新增的属性,在IE9及以下IE浏览器中不支持。网上也有请多解决方法,都比较麻烦,今天和大家分享一个好方法:

var name = "searchKeywords";
var input = document.getElementById(name);
var searchKeywordsDefult = $(input).attr("placeholder");
var searchKeywords = $(input).val();
var notSupport = ('placeholder' in input);
if(notSupport === false){
$(input).bind("focus", onfocusKeywords);
$(input).bind("blur", unfocusKeywords);
$(document).ready(function(){
if(searchKeywords == ""){
$(input).val(searchKeywordsDefult);
}
if(searchKeywords != searchKeywordsDefult){
$(input).css("color","black");
}
});
}
//searchKeywords失去焦点
function unfocusKeywords(){
if($(input).val() == ""){
$(input).val(searchKeywordsDefult);
}
}
//searchKeywords获得焦点
function onfocusKeywords(){
if($(input).val() == searchKeywordsDefult){
$(input).val("");
}

}
原理:首页判断是否支持placeHolder,如果不支持,再设置值,通过获得焦点、失去焦点进行控制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PlaceHolder 兼容性