欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

placeholder在IE8中兼容性问题解决

发布时间:2025/7/14 编程问答 52 豆豆
生活随笔 收集整理的这篇文章主要介绍了 placeholder在IE8中兼容性问题解决 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

placeholder是HTML5中的一个属性,可以在文本框中设置placeholder属性来显示一些提示性的文字,但对IE10以下的浏览器不支持,下面方法可以让placeholder能够使用在IE10以下的版本中。

第一种方法是在页面中添加下面一段脚本:

<script type="text/javascript"> if( !('placeholder' in document.createElement('input')) ){ $('input[placeholder],textarea[placeholder]').each(function(){ var that = $(this), text= that.attr('placeholder'); if(that.val()===""){ that.val(text).addClass('placeholder'); } that.focus(function(){ if(that.val()===text){ that.val("").removeClass('placeholder'); } }) .blur(function(){ if(that.val()===""){ that.val(text).addClass('placeholder'); } }) .closest('form').submit(function(){ if(that.val() === text){ that.val(''); } }); }); } </script>

上面的方法不能支持password类型的文本框,网上找了些解决方法都不是很完美,最后发现jQuery的placeholder插件还不错,代码如下:

https://gist.github.com/oec2003/8946120 将placeholder.js文件引用到页面,页面中添加下面脚本: <script type="text/javascript">$(function() {$('input, textarea').placeholder();}); </script>

 

转载于:https://www.cnblogs.com/whlives/p/3997812.html

总结

以上是生活随笔为你收集整理的placeholder在IE8中兼容性问题解决的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。