zoukankan      html  css  js  c++  java
  • javascript占位符

    传统javascript:

    The text is inserted in the value attribute. On focus, it checks if the value is "search" and returns empty to clear the field. If the value is empty, it returns "search." As you can see, this way is not an efficient way because each field has to be checked.

    <input type="text" value="Search" onfocus="if (this.value == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}">

    Now with HTML5 placeholder, it is more semantic to use placeholder than value attribute. However, placeholder text is not supported by all browsers. To make it cross-browser, Modernizr and jQuery come in handy here.

    Modernizr is used here to check if placeholder is supported. If placeholder is not supported, the jQuery code will run. It finds all elements with placeholder attribute and stores in a variable. It then compares the input value with the placeholder attribute. If the input value is empty, it will display the placeholder text and add a "placeholder" class to the input field. View Demo.

    To use this on your site, download a copy of Modernizr and jQuery and paste the following code any where in your html page (be sure the jquery.js and modernizr.js file is in correct path).

    <script src="jquery.js"></script>
    <script src="modernizr.js"></script>
    
    <script>
    $(document).ready(function(){
    
    if(!Modernizr.input.placeholder){
    
        $('[placeholder]').focus(function() {
          var input = $(this);
          if (input.val() == input.attr('placeholder')) {
            input.val('');
            input.removeClass('placeholder');
          }
        }).blur(function() {
          var input = $(this);
          if (input.val() == '' || input.val() == input.attr('placeholder')) {
            input.addClass('placeholder');
            input.val(input.attr('placeholder'));
          }
        }).blur();
        $('[placeholder]').parents('form').submit(function() {
          $(this).find('[placeholder]').each(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
              input.val('');
            }
          })
        });
    
    }
    
    </script>

    via:http://webdesignerwall.com/tutorials/cross-browser-html5-placeholder-text

    另外一个原生的javascript实现;

    function add_placeholder (id, placeholder)
    {
        var el = document.getElementById(id);
        el.placeholder = placeholder;
     
        el.onfocus = function ()
        {
            if(this.value == this.placeholder)
            {
                this.value = '';
                el.style.cssText  = '';
            }
        };
     
        el.onblur = function ()
        {
            if(this.value.length == 0)
            {
                this.value = this.placeholder;
                el.style.cssText = 'color:#A9A9A9;';
            }
        };
     
        el.onblur();
    }
     
    // Add right before </body> or inside a DOMReady wrapper
    add_placeholder('myInputField', 'IE Placeholder Text');

    <inputtype="text"name="myInputField"value=""placeholder="HTML5 Placeholder Text"id="myInputField">

    参考:https://gist.github.com/jaywilliams/1105055#file-example-html

  • 相关阅读:
    [转]MySql 5.7关键字和保留字-附表
    [转]超链接标签简单的几个样式属性
    layui table 前台数字格式保留两位小数,不足补0(mysql 数据库)
    [转]Object.keys()和for in的排序问题
    [转]对form:input标签中的数字进行格式化
    [转]bootstrap table 动态列数
    [转]bootstrap的table插件动态加载表头
    [转]【MyBatis】Decimal映射到实体类出现科学计数法问题
    [转]MySQL函数大全 及用法示例
    [转]mysql update case when和where之间的注意事项
  • 原文地址:https://www.cnblogs.com/youxin/p/2992210.html
Copyright © 2011-2022 走看看