input type=“text” vs input type=“search” in HTML5

前端 未结 10 1660
無奈伤痛
無奈伤痛 2021-01-30 12:05

I\'m new to HTML5 as begun to work with HTML5\'s new form input fields. When I\'m working with form input fields, especially and

相关标签:
10条回答
  • 2021-01-30 12:31

    It does absolutely nothing in most browsers. It just behaves like a text input. This isn't a problem. The spec doesn't require it to do anything special. WebKit browsers do treat it a bit differently though, primarily with styling.

    A search input in WebKit by default has an inset border, rounded corners, and strict typographic control.

    Also,

    This isn't documented anywhere that I know of nor is it in the spec, but you if you add a results parameter on the input, WebKit will apply a little magnifying glass with a dropdown arrow showing previous results.

    <input type=search results=5 name=s>
    

    Reference

    Above all, it provides a semantic meaning to the input type.

    Update:

    Chrome 51 removed support for the results attribute:

    0 讨论(0)
  • 2021-01-30 12:33

    Actually be very careful about assuming it does nothing. When you go to style inputs with the type search they have certain attributes that cannot be changed. Try to change the border on one and you will find it quite impossible. There are several other disallowed CSS attributes, check out this for all the details.

    Also as mentioned by Jashwant there's the result attribute, though it doesn't work very well unless you also include the autosave attribute. The drop down will not work in most browsers, however, so use at your own peril.

    0 讨论(0)
  • 2021-01-30 12:40

    Bonus point: input type="search" has the ability to use the onsearch attribute (although I have noticed this does NOT work in Microsofts new Edge Browser), which eliminates the need to write a custom onkeypress=if(key=13) { function() } thing.

    0 讨论(0)
  • 2021-01-30 12:41

    Right now, there isn't a huge deal between them - maybe there never will be. However, the point is to give the browser-makers the ability to do something special with it, if they want.

    Think about <input type="number"> on cellphones, bringing up number pads, or type="email" bringing up a special version of the keyboard, with @ and .com and the rest available.

    On a cellphone, search could bring up an internal search applet, if they wanted.

    On the other side, it helps current devs with css.

    input[type=search]:after { content : url("magnifying-glass.gif"); }
    
    0 讨论(0)
  • 2021-01-30 12:41

    using input type="search" make the keybord enterkey's text show "search", which may improve user experience. however, you have to adjust the style if using this type.

    0 讨论(0)
  • 2021-01-30 12:43

    It depends on the programmers point of view, a programmer can easily determine the purpose of the input by looking on the type and it's easy for CSS styling and for JavaScript or JQuery to verify rule in the inputs.

    0 讨论(0)
提交回复
热议问题