How to create a label inside an <input> element?

后端 未结 13 578
悲哀的现实
悲哀的现实 2021-01-29 22:54

I would like to insert a descriptive text inside an input element that disappers when the user click on it.

I know it is a very common trick, but I do not know how to do

相关标签:
13条回答
  • 2021-01-29 23:26

    In my opinion, the best solution involves neither images nor using the input's default value. Rather, it looks something like David Dorward's solution.

    It's easy to implement and degrades nicely for screen readers and users with no javascript.

    Take a look at the two examples here: http://attardi.org/labels/

    I usually use the second method (labels2) on my forms.

    0 讨论(0)
  • 2021-01-29 23:26

    I've put together solutions proposed by @Cory Walker with the extensions from @Rafael and the one form @Tex witch was a bit complicated for me and came up with a solution that is hopefully

    error-proof with javascript and CSS disabled.

    It manipulates with the background-color of the form field to show/hide the label.

    <head>
    
    <style type="text/css">
    <!--
        input {position:relative;background:transparent;} 
    -->
    </style>
    
    <script>
        function labelPosition() {
            document.getElementById("name").style.position="absolute"; 
                // label is moved behind the textfield using the script, 
                // so it doesnt apply when javascript disabled
        }
    </script>
    
    </head>
    <body onload="labelPosition()">
    
    <form>
            <label id="name">Your name</label>
            <input type="text" onblur="if(this.value==''){this.style.background='transparent';}" onfocus="this.style.background='white'">
    </form>
    
    </body>
    

    View the script in action: http://mattr.co.uk/work/form_label.html

    0 讨论(0)
  • 2021-01-29 23:27

    One hint about HTML property placeholder and the tag textarea, please make sure there is no any space between <textarea> and </textarea>, otherwise the placeholder doesn't work, for example:

    <textarea id="inputJSON" spellcheck="false" placeholder="JSON response string" style="flex: 1;"> </textarea>
    

    This won't work, because there is a space between...

    0 讨论(0)
  • 2021-01-29 23:29

    The common approach is to use the default value as a label, and then remove it when the field gains the focus.

    I really dislike this approach as it has accessibility and usability implications.

    Instead, I would start by using a standard element next to the field.

    Then, if JavaScript is active, set a class on an ancestor element which causes some new styles to apply that:

    • Relatively position a div that contains the input and label
    • Absolutely position the label
    • Absolutely position the input on top of the label
    • Remove the borders of the input and set its background-color to transparent

    Then, and also whenever the input loses the focus, I test to see if the input has a value. If it does, ensure that an ancestor element has a class (e.g. "hide-label"), otherwise ensure that it does not have that class.

    Whenever the input gains the focus, set that class.

    The stylesheet would use that classname in a selector to hide the label (using text-indent: -9999px; usually).

    This approach provides a decent experience for all users, including those with JS disabled and those using screen readers.

    0 讨论(0)
  • 2021-01-29 23:30

    Please use PlaceHolder.JS its works in all browsers and very easy for non html5 compliant browsers http://jamesallardice.github.io/Placeholders.js/

    0 讨论(0)
  • 2021-01-29 23:31

    Here is a simple example, all it does is overlay an image (with whatever wording you want). I saw this technique somewhere. I am using the prototype library so you would need to modify if using something else. With the image loading after window.load it fails gracefully if javascript is disabled.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1;" />
        <meta http-equiv="Expires" content="Fri, Jan 1 1981 08:00:00 GMT" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Cache-Control" content="no-cache" />
        <style type="text/css" >
    
            input.searcher
            {
                background-image: url(/images/search_back.png);
                background-repeat: no-repeat;
                background-attachment: scroll;
                background-x-position: left;
                background-y-position: center;
            }
    
        </style>
    
        <script type="text/javascript" src="/logist/include/scripts/js/prototype.js" ></script>
    </head>
    <body>
        <input type="text" id="q" name="q" value="" />
    
        <script type="text/javascript" language="JavaScript" >
        //  <![CDATA[
            function f(e){
                $('q').removeClassName('searcher');
            }
    
            function b(e){
                if ( $F('q') == '' )
                {
                    $('q').addClassName('searcher');
                }
            }
    
            Event.observe( 'q', 'focus', f);
            Event.observe( 'q', 'blur', b);
            Event.observe( window, 'load', b);
    
        //  ]]>
        </script>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题