zoukankan      html  css  js  c++  java
  • placeholder的ie兼容性问题

    在html中引用以下的js文件,且在样式文件里这样写:input,textarea{color:#999;}        input:focus,textarea:focus{color:#333;}

    我这里仅仅是简单的用全局样式这样写了,假设你不想全部的input框都有提示,能够给须要的input特定的类,比方:.txt{color:#999;}   .txt:focus{color:#333;}

    (function($) {

     /**
      * Spoofs placeholders in browsers that don't support them (eg Firefox 3)
      *
      * Copyright 2011 Dan Bentley
      * Licensed under the Apache License 2.0
      *
      * Author: Dan Bentley [github.com/danbentley]
      */

     // Return if native support is available.
     if ("placeholder" in document.createElement("input")) return;

     $(document).ready(function(){
      $(':input[placeholder]').not(':password').each(function() {
       setupPlaceholder($(this));
      });

      $(':password[placeholder]').each(function() {
       setupPasswords($(this));
      });
       
      $('form').submit(function(e) {
       clearPlaceholdersBeforeSubmit($(this));
      });
     });

     function setupPlaceholder(input) {

      var placeholderText = input.attr('placeholder');

      setPlaceholderOrFlagChanged(input, placeholderText);
      input.focus(function(e) {
       if (input.data('changed') === true) return;
       if (input.val() === placeholderText) input.val('');
      }).blur(function(e) {
       if (input.val() === '') input.val(placeholderText);
      }).change(function(e) {
       input.data('changed', input.val() !== '');
      });
     }

     function setPlaceholderOrFlagChanged(input, text) {
      (input.val() === '') ? input.val(text) : input.data('changed', true);
     }

     function setupPasswords(input) {
      var passwordPlaceholder = createPasswordPlaceholder(input);
      input.after(passwordPlaceholder);

      (input.val() === '') ? input.hide() : passwordPlaceholder.hide();

      $(input).blur(function(e) {
       if (input.val() !== '') return;
       input.hide();
       passwordPlaceholder.show();
      });
       
      $(passwordPlaceholder).focus(function(e) {
       input.show().focus();
       passwordPlaceholder.hide();
      });
     }

     function createPasswordPlaceholder(input) {
      return $('<input>').attr({
       placeholder: input.attr('placeholder'),
       value: input.attr('placeholder'),
       id: input.attr('id'),
       readonly: true
      }).addClass(input.attr('class'));
     }

     function clearPlaceholdersBeforeSubmit(form) {
      form.find(':input[placeholder]').each(function() {
       if ($(this).data('changed') === true) return;
       if ($(this).val() === $(this).attr('placeholder')) $(this).val('');
      });
     }
    })(jQuery);

  • 相关阅读:
    代码题(22)— 二叉树镜像、相同的树 、对称二叉树
    代码题(26)— 不同路径
    代码题(25)— 最大子序和、最长上升子序列
    Linux 基本命令总结
    C++(五)— 控制保留小数位数
    C++(四)— 字符串、数字翻转3种方法
    代码题(24)— 寻找重复数、数组中重复的数据、找到所有数组中消失的数字
    代码题(23)— 数组中的最长山脉
    【vue】vue +element 搭建项目,将js函数变成vue的函数
    【vue】vue +element 搭建项目,$createElement使用
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7065603.html
Copyright © 2011-2022 走看看