解决IE不支持placeholder

待我称王封你为后i 2022-05-14 01:37 693阅读 0赞

一.解决方法

  1. /*
  2. * jQuery placeholder, fix for IE6,7,8,9
  3. */
  4. var JPlaceHolder = {
  5. //检测
  6. _check : function(){
  7. return 'placeholder' in document.createElement('input');
  8. },
  9. //初始化
  10. init : function(){
  11. if(!this._check()){
  12. this.fix();
  13. }
  14. },
  15. //修复
  16. fix : function(){
  17. jQuery(':input[placeholder]').each(function(index, element) {
  18. var self = $(this), txt = self.attr('placeholder');
  19. self.wrap($('<div></div>').css({position:'relative', zoom:'1', border:'none', background:'none', padding:'none', margin:'none'}));
  20. var pos = self.position(), h = self.outerHeight(true), paddingleft = self.css('padding-left');
  21. var holder="";
  22. if($(this).val() == ""){
  23. holder = $('<span></span>').text(txt).css({position:'absolute', left:pos.left, top:pos.top, height:h, lienHeight:h, paddingLeft:paddingleft, color:'#aaa'}).appendTo(self.parent());
  24. }else{
  25. holder = $('<span></span>').text("").css({position:'absolute', left:pos.left, top:pos.top, height:h, lienHeight:h, paddingLeft:paddingleft, color:'#aaa'}).appendTo(self.parent());
  26. }
  27. self.focusin(function(e) {
  28. holder.hide();
  29. }).focusout(function(e) {
  30. if(!self.val()){
  31. holder.show();
  32. }
  33. });
  34. holder.click(function(e) {
  35. holder.hide();
  36. self.focus();
  37. });
  38. });
  39. }
  40. };
  41. //执行
  42. jQuery(function(){
  43. JPlaceHolder.init();
  44. });

发表评论

表情:
评论列表 (有 0 条评论,693人围观)

还没有评论,来说两句吧...

相关阅读