PlaceHolder 是HTML5新增的属性,在IE9及如下IE浏览器中不支持。网上也有请多解决方法,都比较麻烦,今天和你们分享一个好方法:javascript
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,若是不支持,再设置值,经过得到焦点、失去焦点进行控制。