2011-12-25 91 views
22

以下格式的文本框的占位符对于Internet Explorer无效。无论如何要在Internet Explorer中显示TextBox的占位符?不适用于Internet Explorer的占位符

<asp:TextBox id="email" runat="server" width="300" placeholder="Your email" />

有没有什么简单的方法只需使用任何JavaScript。我不想使用jQuery使它变得复杂。

+0

http://stackoverflow.com/questions/5120257/what-is-the-best-html5-placeholder-like-jquery-plugin-out-there – noob 2011-12-25 06:37:42

+1

[占位符属性的可能重复**不应该**作为一种替代的标签(http://www.w3.org/TR/html5/common-input-element-attributes.html#the-placeholder-attribute) – Quentin 2012-11-12 16:07:40

+0

更改行#11至:如果( !curInput.type || curInput.type == “” || curInput.type == “文本” || curInput.type == “密码”)...和它的作品为密码字段,以及! – 2012-11-12 16:04:57

回答

50

你可以模仿这种使用纯JavaScript:

var _debug = false; 
var _placeholderSupport = function() { 
    var t = document.createElement("input"); 
    t.type = "text"; 
    return (typeof t.placeholder !== "undefined"); 
}(); 

window.onload = function() { 
    var arrInputs = document.getElementsByTagName("input"); 
    var arrTextareas = document.getElementsByTagName("textarea"); 
    var combinedArray = []; 
    for (var i = 0; i < arrInputs.length; i++) 
     combinedArray.push(arrInputs[i]); 
    for (var i = 0; i < arrTextareas.length; i++) 
     combinedArray.push(arrTextareas[i]); 
    for (var i = 0; i < combinedArray.length; i++) { 
     var curInput = combinedArray[i]; 
     if (!curInput.type || curInput.type == "" || curInput.type == "text" || curInput.type == "textarea") 
      HandlePlaceholder(curInput); 
     else if (curInput.type == "password") 
      ReplaceWithText(curInput); 
    } 

    if (!_placeholderSupport) { 
     for (var i = 0; i < document.forms.length; i++) { 
      var oForm = document.forms[i]; 
      if (oForm.attachEvent) { 
       oForm.attachEvent("onsubmit", function() { 
        PlaceholderFormSubmit(oForm); 
       }); 
      } 
      else if (oForm.addEventListener) 
       oForm.addEventListener("submit", function() { 
        PlaceholderFormSubmit(oForm); 
       }, false); 
     } 
    } 
}; 

function PlaceholderFormSubmit(oForm) {  
    for (var i = 0; i < oForm.elements.length; i++) { 
     var curElement = oForm.elements[i]; 
     HandlePlaceholderItemSubmit(curElement); 
    } 
} 

function HandlePlaceholderItemSubmit(element) { 
    if (element.name) { 
     var curPlaceholder = element.getAttribute("placeholder"); 
     if (curPlaceholder && curPlaceholder.length > 0 && element.value === curPlaceholder) { 
      element.value = ""; 
      window.setTimeout(function() { 
       element.value = curPlaceholder; 
      }, 100); 
     } 
    } 
} 

function ReplaceWithText(oPasswordTextbox) { 
    if (_placeholderSupport) 
     return; 
    var oTextbox = document.createElement("input"); 
    oTextbox.type = "text"; 
    oTextbox.id = oPasswordTextbox.id; 
    oTextbox.name = oPasswordTextbox.name; 
    //oTextbox.style = oPasswordTextbox.style; 
    oTextbox.className = oPasswordTextbox.className; 
    for (var i = 0; i < oPasswordTextbox.attributes.length; i++) { 
     var curName = oPasswordTextbox.attributes.item(i).nodeName; 
     var curValue = oPasswordTextbox.attributes.item(i).nodeValue; 
     if (curName !== "type" && curName !== "name") { 
      oTextbox.setAttribute(curName, curValue); 
     } 
    } 
    oTextbox.originalTextbox = oPasswordTextbox; 
    oPasswordTextbox.parentNode.replaceChild(oTextbox, oPasswordTextbox); 
    HandlePlaceholder(oTextbox); 
    if (!_placeholderSupport) { 
     oPasswordTextbox.onblur = function() { 
      if (this.dummyTextbox && this.value.length === 0) { 
       this.parentNode.replaceChild(this.dummyTextbox, this); 
      } 
     }; 
    } 
} 

function HandlePlaceholder(oTextbox) { 
    if (!_placeholderSupport) { 
     var curPlaceholder = oTextbox.getAttribute("placeholder"); 
     if (curPlaceholder && curPlaceholder.length > 0) { 
      Debug("Placeholder found for input box '" + oTextbox.name + "': " + curPlaceholder); 
      oTextbox.value = curPlaceholder; 
      oTextbox.setAttribute("old_color", oTextbox.style.color); 
      oTextbox.style.color = "#c0c0c0"; 
      oTextbox.onfocus = function() { 
       var _this = this; 
       if (this.originalTextbox) { 
        _this = this.originalTextbox; 
        _this.dummyTextbox = this; 
        this.parentNode.replaceChild(this.originalTextbox, this); 
        _this.focus(); 
       } 
       Debug("input box '" + _this.name + "' focus"); 
       _this.style.color = _this.getAttribute("old_color"); 
       if (_this.value === curPlaceholder) 
        _this.value = ""; 
      }; 
      oTextbox.onblur = function() { 
       var _this = this; 
       Debug("input box '" + _this.name + "' blur"); 
       if (_this.value === "") { 
        _this.style.color = "#c0c0c0"; 
        _this.value = curPlaceholder; 
       } 
      }; 
     } 
     else { 
      Debug("input box '" + oTextbox.name + "' does not have placeholder attribute"); 
     } 
    } 
    else { 
     Debug("browser has native support for placeholder"); 
    } 
} 

function Debug(msg) { 
    if (typeof _debug !== "undefined" && _debug) { 
     var oConsole = document.getElementById("Console"); 
     if (!oConsole) { 
      oConsole = document.createElement("div"); 
      oConsole.id = "Console"; 
      document.body.appendChild(oConsole); 
     } 
     oConsole.innerHTML += msg + "<br />"; 
    } 
} 

这将做什么,如果浏览器已经支持placeholder属性,并且如果它不支持(例如,浏览器是IE),它会很加类似的功能 - 默认显示的文字会在焦点上消失,如果用户没有输入任何内容,则会再次出现。

Live test case

Bug修复

2012年11月6日:以前的代码无法检测时,浏览器没有为占位符的原生支持。使用找到的代码in this other post现在已修复。受影响的浏览器:IE7和IE8,或许还有其他的。还增加了调试支持来帮助调试未来的问题。

2013年1月30日

  1. 添加密码输入支持。由于IE8及以下版本不允许动态更改输入类型,因此只要没有输入密码,代码就会使用文本输入替换密码输入,因此占位符文本将可见。

  2. 修复了在提交与输入相关联的表单时发送空值应发送到服务器的占位符值的问题。为了达到这个目的,代码被附加到表单提交事件上,并在需要时清除该值。

2014年1月24日:增加对<textarea>元件的支持。

+2

哇谢谢,我绕过整个互联网寻找一个在IE中工作的占位符回退。最后,这是一个。 – Baseer 2012-05-07 02:49:23

+0

@Baseer欢呼,很高兴看到它被使用。 :) – 2012-05-09 21:46:48

+0

@ShadowWizard这不适用于IE7或IE8。它适用于IE9,但不适用于密码输入。 – 2012-08-09 23:55:56

0

占位符是HTML5功能。要解决我发现MSIE和做到这一点:

if ($.browser.msie) { 
    $("#textarea-id").val('placeholder'); 
    $("#textarea-id").focus(function(){ 
     this.select(); 
    }); 
} 

它的jQuery但并不是很复杂......

+0

我不使用HTML5 :( – 2011-12-25 07:42:11

+1

它可能不是那么复杂,但为什么要使用31KB库仅此expletively简单的任务有几个?几百个字节的纯JS似乎吸尘器我:) – 2011-12-25 11:57:51

+0

@reanimation,固点 – 2011-12-25 13:23:51

3

有许多书面填充工具脚本,将添加占位符支持本身不浏览器支持该技术。

而不是重复什么在其他地方写的,我建议你去这里:https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills和向下滚动有关的1/3方式部分标题为Web窗体:输入占位符一些不同的选项(包括本地JS和jQuery)。由于整个页面由HTML5 Boilerplate团队策划,因此您可以相当肯定所提供的脚本质量不错。

编辑:刚刚看到您关于不使用HTML5的评论。我提供的链接上的脚本应该仍然工作,即使您不使用HTML5文档类型(不保证明示或暗示等)。

0

最近我有这个问题 - 我使用Modernizr的检测HTML5的特性,然后跑了一下JS的对于不能应付的浏览器:

function addPlaceHolder(input) {//using modernizr add placeholder text for non html5 users 
    if (!Modernizr.input.placeholder) { 
     input.focus(function() { 
      if (input.val() == input.attr('placeholder')) { 
       input.val(''); 
       input.removeClass('placeholder'); 
      } 
     }).blur(function() { 
      if (input.val() == '' || input.val() == input.attr('placeholder')) { 
       input.addClass('placeholder'); 
       input.val(input.attr('placeholder')); 
      } 
     }).blur(); 
     $(input).parents('form').submit(function() { 
      $(this).find(input).each(function() { 
       if (input.val() == input.attr('placeholder')) { 
        input.val(''); 
       } 
      }) 
     }); 
    } 
} 

addPlaceHolder($('#Myinput')); 

似乎很适合我的工作!

-1

刚刚从上面抓住了代码,并使其更通用

<script type="text/javascript"> 


function addPlaceHolder(input) { 

    if (!Modernizr.input.placeholder) { 
     input.focus(function() { 
      if ($(this).val() == $(this).attr('placeholder')) { 
       $(this).val(''); 
       $(this).removeClass('placeholder'); 
      } 
     }).blur(function() { 
      if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) { 
       $(this).addClass('placeholder'); 
       $(this).val($(this).attr('placeholder')); 
      } 
     }).blur(); 
     $(input).parents('form').submit(function() { 
      $(this).find(input).each(function() { 
       if ($(this).val() == $(this).attr('placeholder')) { 
        $(this).val(''); 
       } 
      }) 
     }); 
    } 
} 

    addPlaceHolder($(':text')); 
    addPlaceHolder($('textarea')); 

</script> 
+0

原帖声明,他不想使用jQuery。 – Scottie 2013-05-10 07:27:41

3

我已经写了这个简单的代码,并进行测试时,我工作得很好:

placeholder=function(){ 
    $('input, textarea').each(function(){ 
     var holder=$(this).attr('placeholder'); 
     if(typeof(holder) !=='undefined'){ 
      $(this).val(holder); 
      $(this).bind('click',function(){ 
       $(this).val(''); 
      }).blur(function(){ 
       $(this).val(holder); 
      }); 
     } 
    }); 
}; 
$(document).ready(placeholder); 
+0

您的代码在ie中工作,但在FF和GC浏览器中占位符文本变得粗体,如果我点击正常字体出现在输入框中,我不打包查看输入的数据。对于调试,我删除了DOM从底部开始添加并添加到顶部,FF&GC中的问题看起来很好,但占位符不在IE中。 – user2086641 2013-08-28 07:55:01

+0

优秀的东西!但是我已经把函数初始化放入了一个条件,比如'if($ .browser.msie && $ .browser.version <='9.0')'。非常感谢! – 2014-01-21 17:30:29

0

可以使用JavaScript Polyfill,使旧版浏览器上的占位符的工作。那里有很多Polyfills。这里是一个谈到Making Placeholders work in IE。基本上,所有这些脚本所做的就是模拟使用JavaScript的浏览器原生占位符支持的行为。

相关问题