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,如果不支持,再设置值,通过获得焦点、失去焦点进行控制。
相关文章推荐
- css 兼容性问题this.style.cursor=''hand''
- Firefox和IE兼容性问题及解决方法总结
- Dom与浏览器兼容性说明
- 编写针对IE的JS代码两种编写方法
- IE和Firefox在JavaScript应用中的兼容性探讨
- js兼容的placeholder属性详解
- 实现placeholder效果的方案汇总
- jQuery实现的一个自定义Placeholder属性插件
- 两种方法基于jQuery实现IE浏览器兼容placeholder效果
- javascript在事件监听方面的兼容性小结
- 网页和浏览器兼容性问题汇总(draft1)
- 原生javascript兼容性测试实例
- 基于jQuery的让非HTML5浏览器支持placeholder属性的代码
- IE和Firefox的Javascript兼容性总结[推荐收藏]
- FF IE兼容性的修改小结
- 整理一些JavaScript的IE和火狐的兼容性注意事项
- Optimizer与Debugger兼容性问题的解决方法
- JavaScript中解决多浏览器兼容性23个问题的快速解决方法
- 谈谈对offsetleft兼容性的理解
- ASP.NET中 PlaceHolder 控件的使用方法