这似乎是一个众所周知的问题,但我在 Google 上找到的所有解决方案都不适用于我新下载的 IE9。
您最喜欢哪种方式来启用和标签Placeholder上的属性?inputtextarea
可选:我在这件事上浪费了很多时间,还没有寻找required房产。您对此也有什么建议吗?显然我可以在 PHP 中检查该值,但是这个属性对于帮助用户来说非常方便。
这似乎是一个众所周知的问题,但我在 Google 上找到的所有解决方案都不适用于我新下载的 IE9。
您最喜欢哪种方式来启用和标签Placeholder上的属性?inputtextarea
可选:我在这件事上浪费了很多时间,还没有寻找required房产。您对此也有什么建议吗?显然我可以在 PHP 中检查该值,但是这个属性对于帮助用户来说非常方便。
HTML5 Placeholder jQuery 插件
- Mathias Bynens(HTML5 Boilerplate和jsPerf 的合作者)
https://github.com/mathiasbynens/jquery-placeholder
演示和示例
http://mathiasbynens.be/demo/placeholder
ps
这个插件我用过很多次了,效果很好。当您提交表单时,它也不会将占位符文本作为值提交(......我发现其他插件真的很痛苦)。
我认为这就是你要找的:jquery-html5-placeholder-fix
此解决方案使用功能检测(通过modernizr)来确定是否支持占位符。如果没有,添加支持(通过 jQuery)。
如果你想在不使用 jquery 或 modenizer 的情况下做到这一点,你可以使用下面的代码:
(function(){
     "use strict";
     //shim for String's trim function..
     function trim(string){
         return string.trim ? string.trim() : string.replace(/^\s+|\s+$/g, "");
     }
     //returns whether the given element has the given class name..
     function hasClassName(element, className){ 
         //refactoring of Prototype's function..
         var elClassName = element.className;
         if(!elClassName)
             return false;
         var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
         return regex.test(element.className);
     }
     function removeClassName(element, className){
         //refactoring of Prototype's function..
         var elClassName = element.className;
         if(!elClassName)
             return;
         element.className = elClassName.replace(
             new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ');
     }
     function addClassName(element, className){
         var elClassName = element.className;
         if(elClassName)
             element.className += " " + className;
         else
             element.className = className;
     }
     //strings to make event attachment x-browser.. 
     var addEvent = document.addEventListener ?
            'addEventListener' : 'attachEvent';
     var eventPrefix = document.addEventListener ? '' : 'on';
     //the class which is added when the placeholder is being used..
     var placeHolderClassName = 'usingPlaceHolder';
     //allows the given textField to use it's placeholder attribute
     //as if it's functionality is supported natively..
     window.placeHolder = function(textField){
         //don't do anything if you get it for free..
         if('placeholder' in document.createElement('input'))
             return;
         //don't do anything if the place holder attribute is not
         //defined or is blank..
         var placeHolder = textField.getAttribute('placeholder');        
         if(!placeHolder)
             return;
         //if it's just the empty string do nothing..
         placeHolder = trim(placeHolder);
         if(placeHolder === '')
             return;
         //called on blur - sets the value to the place holder if it's empty..
         var onBlur = function(){
             if(textField.value !== '') //a space is a valid input..
                 return;
             textField.value = placeHolder;
             addClassName(textField, placeHolderClassName);
         };
         //the blur event..
         textField[addEvent](eventPrefix + 'blur', onBlur, false);
         //the focus event - removes the place holder if required..
         textField[addEvent](eventPrefix + 'focus', function(){
             if(hasClassName(textField, placeHolderClassName)){
                removeClassName(textField, placeHolderClassName);
                textField.value = "";
             }
         }, false);
         //the submit event on the form to which it's associated - if the
         //placeholder is attached set the value to be empty..
         var form = textField.form;
         if(form){
             form[addEvent](eventPrefix + 'submit', function(){
                 if(hasClassName(textField, placeHolderClassName))
                     textField.value = '';
            }, false);
         }
         onBlur(); //call the onBlur to set it initially..
    };
}());
对于您想要使用它的每个文本字段,您都需要运行placeHolder(HTMLInputElement),但我想您可以更改它以适应!此外,这样做,而不仅仅是在加载时意味着您可以使其在页面加载时适用于不在 DOM 中的输入。
请注意,这是通过将 class:usingPlaceHolder应用于输入元素来工作的,因此您可以使用它来设置样式(例如添加规则.usingPlaceHolder { color: #999; font-style: italic; }以使其看起来更好)。
这是一个更好的解决方案。 http://bavotasan.com/2011/html5-placeholder-jquery-fix/ 我已经采用了一点,仅适用于 IE10 下的浏览器
<!DOCTYPE html>
<!--[if lt IE 7]><html class="no-js lt-ie10 lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]><html class="no-js lt-ie10 lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]><html class="no-js lt-ie10 lt-ie9" lang="en"> <![endif]-->
<!--[if IE 9]><html class="no-js lt-ie10" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en"><!--<![endif]-->
    <script>
    // Placeholder fix for IE
      $('.lt-ie10 [placeholder]').focus(function() {
        var i = $(this);
        if(i.val() == i.attr('placeholder')) {
          i.val('').removeClass('placeholder');
          if(i.hasClass('password')) {
            i.removeClass('password');
            this.type='password';
          }     
        }
      }).blur(function() {
        var i = $(this);  
        if(i.val() == '' || i.val() == i.attr('placeholder')) {
          if(this.type=='password') {
            i.addClass('password');
            this.type='text';
          }
          i.addClass('placeholder').val(i.attr('placeholder'));
        }
      }).blur().parents('form').submit(function() {
        //if($(this).validationEngine('validate')) { // If using validationEngine
          $(this).find('[placeholder]').each(function() {
            var i = $(this);
            if(i.val() == i.attr('placeholder'))
              i.val('');
              i.removeClass('placeholder');
          })
        //}
      });
    </script>
    ...
    </html>
如果你想输入一个描述,你可以使用它。这适用于 IE 9 和所有其他浏览器。
<input type="text" onclick="if(this.value=='CVC2: '){this.value='';}" onblur="if(this.value==''){this.value='CVC2: ';}" value="CVC2: "/>