IE9中的占位符

chr*_*tfr 139 html javascript html5 placeholder internet-explorer-9

这似乎是一个众所周知的问题,但我在Google上找到的所有解决方案都不适用于我新下载的IE9.

为了Placeholderinputtextarea标签上启用属性,您最喜欢哪种方式?

可选:我在这上面浪费了很多时间,但还没有寻找required房产.你也对此有一些建议吗?显然我可以检查PHP中的值,但是为了帮助用户这个属性非常方便.

Chr*_*cob 184

HTML5占位符jQuery插件
- 由Mathias Bynens(HTML5 BoilerplatejsPerf的合作者)

https://github.com/mathiasbynens/jquery-placeholder

演示和示例

http://mathiasbynens.be/demo/placeholder

ps
我已经多次使用过这个插件而且它很有效.此外,当您提交表单时,它不会将占位符文本作为值提交(...这是我在其他插件中发现的真正痛苦).

  • 这确实是一种享受,但是在单页应用程序上使用它会很痛苦.您需要手动触发动态添加的字段.除此之外,效果很好! (2认同)

mat*_*att 21

我认为这就是你要找的东西:jquery-html5-placeholder-fix

此解决方案使用特征检测(通过modernizr)来确定是否支持占位符.如果没有,添加支持(通过jQuery).

  • 您必须小心使用此代码,因为如果您提交表单,占位符值将作为表单值提交.应该在提交之前清除它. (20认同)
  • 它有效,但它不是最好的代码.在顶部`$(this)`应该分配给一个可以在整个代码中使用的变量.为每个使用`$(this)`的表达式调用`$`是"不要写像这个101的jQuery". (2认同)
  • @chriscatfr请考虑接受我的回答(http://stackoverflow.com/a/13281620/114140),因为我觉得它会引导SO用户找到更好的解决方案. (2认同)

Mar*_*des 17

如果你想在不使用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..
    };

}());
Run Code Online (Sandbox Code Playgroud)

对于你想要使用它的每个文本字段,你需要运行placeHolder(HTMLInputElement),但我想你可以改变它以适应!另外,这样做,而不仅仅是加载意味着你可以使它适用于页面加载时不在DOM中的输入.

请注意,这可以通过将类usingPlaceHolder应用于输入元素来实现,因此您可以使用它来设置样式(例如,添加规则.usingPlaceHolder { color: #999; font-style: italic; }以使其看起来更好).


小智 8

这是一个更好的解决方案. 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>
Run Code Online (Sandbox Code Playgroud)


小智 5

如果要输入说明,可以使用此功能.这适用于IE 9和所有其他浏览器.

<input type="text" onclick="if(this.value=='CVC2: '){this.value='';}" onblur="if(this.value==''){this.value='CVC2: ';}" value="CVC2: "/>
Run Code Online (Sandbox Code Playgroud)