Solution to HTML5 placeholder

Source: Internet
Author: User

There are many methods to make earlier browsers support Placeholder, which are not perfect. They are more or less problematic, and some native browsers will clear the Placeholder prompt when getting the focus. I found that zhihu network has a good solution.

In windows, the following browsers are tested: Google Chrome 18, Firefox 12, IE 9, IE 6, and Opera 11.5. The android 4.0 system also tested that the native browser and Opera lele12 basically passed. The defect is that the Placeholder prompt will be cleared when the focus is obtained.

Jquery. placeholder. zhihu. js part:

 

The Code is as follows: Copy code
/*
* Html5 placeholder pollfill
*-Use absolute positioning of the embedded Layer
*-Applicable to password Domains
* Target browser: IE 6 ~ 9, FF 3.5
'''
// Default
$ ('Input [placeholder] '). placeholder ()
 
// When autofocus and placeholder are used together, the prompt text is cleared for non-webkit.
$ ('Input [placeholder] '). placeholder ({
// The original placehodler attribute will be deleted and mandatory to be replaced by JS
UseNative: false,
// The prompt text is not cleared during focus, and the prompt text is cleared only when keypress is valid.
HideOnFocus: false,
// Additional Style
Style :{
TextShadow: 'none'
}
})
'''
*/
(Function ($ ){
Var attr = 'placeholder ', nativeSupported = attr in document. createElement ('input ')
 
$. Fn. placeholder = function (options ){
Return this. each (function (){
Var $ input = $ (this)
 
If (typeof options = 'string '){
Options = {text: options}
}
 
Var opt = $. extend ({
Text :'',
Style :{},
Namespace: 'placeholder ',
UseNative: true,
HideOnFocus: true
}, Options | {})
 
If (! Opt. text ){
Opt. text = $ input. attr (attr)
}
 
If (! Opt. useNative ){
$ Input. removeAttr (attr)
} Else if (nativeSupported ){
// Only change the text
$ Input. attr (attr, opt. text)
Return
}
 
Var width = $ input. width (), height = $ input. height ()
Var box_style = ['margintop', 'marginleft', 'pyaddingtop', 'pyaddingleft', 'pyaddingright']
 
Var show = function () {$ layer. show ()}
Var hide = function () {$ layer. hide ()}
Var is_empty = function () {return! $ Input. val ()}
Var check = function () {is_empty ()? Show (): hide ()}
 
Var position = function (){
Var pos = $ input. position ()
If (! Opt. hideOnFocus ){
// Press ?? What is the need to transfer the data? Yong? Siro
Pos. left + = 2
}
$Layer.css (pos)
$. Each (box_style, function (I, name ){
$Layer.css (name, $input.css (name ))
})
}
 
Var layer_style = {
Color: 'Gray ',
Cursor: 'text ',
TextAlign: 'left ',
Position: 'absolute ',
FontSize: $input.css ('fontsize '),
FontFamily: $input.css ('fontfamily '),
Display: is_empty ()? 'Block': 'none'
}
 
// Create
Var layer_props = {
Text: opt. text,
Width: width,
Height: 'auto'
}
 
// Make sure to bind only once
Var ns = '.' + opt. namespace, $ layer = $ input. data ('lay' + ns)
If (! $ Layer ){
$ Input. data ('lay' + ns, $ layer = $ ('<div>', layer_props). appendTo ($ input. offsetParent ()))
}
 
// Activate
$ Layer
. Css ($. extend (layer_style, opt. style ))
. Unbind ('click' + ns)
. Bind ('click' + ns, function (){
Opt. hideOnFocus & hide ()
$ Input. focus ()
})
 
$ Input
. Unbind (ns)
. Bind ('blur' + ns, check)
 
If (opt. hideOnFocus ){
$ Input. bind ('focal '+ ns, hide)
} Else {
$ Input. bind ('keypress keylow' + ns, function (e ){
Var key = e. keyCode
If (e. charCode | (key >=65 & key <= 90 )){
Hide ()
}
})
. Bind ('keyup' + ns, check)
}
 
// Because ie remembers the password, the listening value needs to be changed.
// Ie9 does not support jq bind.
$ Input. get (0). onpropertychange = check
 
Position ()
Check ()
})
}
 
}) (JQuery)

Html section:

The Code is as follows: Copy code

 

<! DOCTYPE html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<! -- [If lt IE 9]>
<Script src = "js/html5shiv. js"> </script>
<! [Endif] -->
<Title> HTML5 placeholder jQuery Plugin </title>
<Style>
Body, input, textarea {font: 1em/1.4 Helvetica, Arial ;}
Label code {cursor: pointer; float: left; width: 150px ;}
Input {width: 14em ;}
Textarea {height: 5em; width: 20em ;}
. Placeholder {color: # aaa ;}
. Note {border: 1px solid orange; padding: 1em; background: # ffffe0 ;}
</Style>
</Head>
<Body>
<H1> HTML5 Placeholder jQuery Plugin <Form id = "test">
<P> <label> <code> type = search </code> <input type = "search" name = "search" placeholder = "Search this site... "Autofocus> </label> </p>
<P> <label> <code> type = text </code> <input type = "text" name = "name" placeholder = "e.g. john Doe "> </label> </p>
<P> <label> <code> type = email </code> <input type = "email" name = "email" placeholder = "e.g. address@example.ext "> </label> </p>
<P> <label> <code> type = url </code> <input type = "url" name = "url" placeholder = "e.g. http://mathiasbynens.be/"> </label> </p>
<P> <label> <code> type = tel </code> <input type = "tel" name = "tel" placeholder = "e.g. + 32 472 77 69 88 "> </label> </p>
<P> <label for = "p"> <code> type = password </code> </label> <input type = "password" name = "password" placeholder =" e.g. hunter2 "id =" p "> </p>
<P> <label> <code> textarea </code> <textarea name = "message" placeholder = "Your message goes here"> </textarea> </label> </ p>
<P> <input type = "submit" value = "type = submit"> </p>
</Form>
<Script src = "js/jquery-1.7.2.min.js"> </script>
<Script src = "js/jquery. placeholder. zhihu. js"> </script>
<Script>
$ (Function (){
Var support = (function (input ){
Return function (attr) {return attr in input}
}) (Document. createElement ('input '))
If (! (Support ('holder') & $. browser. webkit )){
$ ('Input [placeholder], textarea [placeholder] '). placeholder ({
UseNative: false,
HideOnFocus: false,
Style :{
TextShadow: 'none'
}
});
}
 
If (! Support ('autofocal ')){
$ ('Input [autofocus] '). focus ()
}
});
</Script>
</Body>
</Html>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.