How do I get placeholder text in firefox and other browsers that don't support the html5 tag option?

后端 未结 10 1445
借酒劲吻你
借酒劲吻你 2020-12-02 19:21

This works in Chrome and any other browser that supports placeholder text in HTML5



        
相关标签:
10条回答
  • 2020-12-02 19:38

    I use this one: https://github.com/danbentley/placeholder
    Lightweight and simple jQuery plugin.

    0 讨论(0)
  • 2020-12-02 19:38

    Here is the simplest solution that I found working everywhere:

    <input id="search" 
       name="search" 
       onblur="if (this.value == '') {this.value = 'PLACEHOLDER';}" 
       onfocus="if (this.value == 'PLACEHOLDER') {this.value = '';}"
    />
    

    Replace PLACEHOLDER with your own.

    At the moment, FF3 does not yet support the "placeholder" attribute of the "input" element. FF4, Opera11 and Chrome8 support it partially, i.e. they render the placeholder text in the field, but do not delete it when the user focuses in the field, which is worse that not supporting it at all.

    0 讨论(0)
  • 2020-12-02 19:42

    I use the following snippet that I wrote with jQuery. Just add a class of textbox-auto-clear to any textbox on the page and you should be good to go.

    <input type="text" value="Please enter your name" class="textbox-auto-clear" />
    
    $(".textbox-auto-clear").each(function(){
        var origValue = $(this).val(); // Store the original value
        $(this).focus(function(){
            if($(this).val() == origValue) {
                $(this).val('');
            }
        });
        $(this).blur(function(){
            if($(this).val() == '') {
                $(this).val(origValue);
            }
        });
    });
    

    I assume that you want to keep using the placeholder attribute for HTML5 browsers, in which case you'd have to do some browser detection and only apply the jQuery solution to browsers that don't support it.

    Better yet, you can us the Modernizer library, as outlined in this answer. Detecting support for specific HTML 5 features via jQuery

    0 讨论(0)
  • 2020-12-02 19:46

    Here is a MooTools Plugin, that brings the placeholder to browsers that don't support it yet:

    http://mootools.net/forge/p/form_placeholder

    0 讨论(0)
  • 2020-12-02 19:48

    The trick is to use javascript functions onBlur() and onFocus().

    Here is the code that worked for me:

    <script language="javascript" type="text/javascript" >
    
        var hint_color = "grey", field_color = null;
    
        var hinted = true;
    
        function hint() { // set the default text
    
            document.getElementById('mce-EMAIL').style.color = hint_color;
            document.getElementById('mce-EMAIL').value = "<?php echo SUBSCRIPTION_HINT; ?>";
    
            hinted = true;
    
        }
    
        function hintIfEmpty() { // set the default text, only if the field is empty
    
            if (document.getElementById('mce-EMAIL').value == '') hint();
    
        }
    
        function removeHint() {
    
            if (hinted) {
    
                document.getElementById('mce-EMAIL').style.color = field_color;
                document.getElementById('mce-EMAIL').value = "";
    
                hinted = false;
    
            }
    
        }
    
        function send() {
    
            document.getElementById('subscription_form').submit();
            hint();
    
        }
    
    </script>
    
    <div style="position:absolute; display: block; top:10; left:10; ">
    <form id="subscription_form" action="<?php echo SUBSCRIPTION_LINK; ?>" method="post" target="_blank">
    
        <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" style="width: 122px;" onBlur="hintIfEmpty();" onFocus="removeHint();" required>
        <a href="javascript:send()"><font style="position: relative; top:-1px;"><b>ok</b></font></a>
    
    </form>
    </div>
    
    <script language="javascript" type="text/javascript" >
    
        field_color = document.getElementById('mce-EMAIL').style.color;
        hint();
    
    </script>
    

    SUBSCRIPTION_HINT (i.e.: "your e-mail" ) and SUBSCRIPTION_LINK (i.e.: the value of the 'action' tag in your EN mailchimp embed code...) are PHP constants used for localization.

    0 讨论(0)
  • Works for me, change your CSS to

    ::-webkit-input-placeholder {
      color: #999;
    }
    
    0 讨论(0)
提交回复
热议问题