/* * HTML5 placeholder Pollfill *-use absolute positioning inline layer *-also applies to password fields * Target browser: IE 6~9, FF 3.5 ``` Default $ (' input[placeholder] '). Placeholder ()
Autofocus and placeholder collocation, WebKit empty the prompt text, recommended $ (' input[placeholder] '). Placeholder ({ The original Placehodler attribute will be deleted, and JS is enforced instead Usenative:false, Focus does not clear the prompt text, keypress valid word Fu Shicai empty Hideonfocus:false, Additional styles 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) { Change Text Only $input. attr (attr, Opt.text) Return }
var width = $input. Width (), height = $input. Height () var box_style = [' margintop ', ' marginleft ', ' paddingtop ', ' paddingleft ', ' paddingright ']
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) { By?? In the case of possession, it is necessary to move the eligible for. 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 (' layer ' + ns) if (! $layer) { $input. Data (' layer ' + 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 (' Focus ' + ns, hide) }else{ $input. Bind (' keypress keydown ' + NS, function (e) { var key = E.keycode if (E.charcode | | (Key >= && key <=90)) { Hide () } }) . bind (' KeyUp ' + ns,check) }
Because IE remembers the character of the password, it needs to change the listening value. IE9 does not support JQ bind this event $input. Get (0). Onpropertychange = Check
Position () Check () }) }
}) (JQuery) |