JQuery plug-in that implements the placeholder effect across browsers, placeholderjquery

Source: Internet
Author: User

JQuery plug-in that implements the placeholder effect across browsers, placeholderjquery

Once encountered such a problem, handling IE8 Password box placeholder attribute compatibility. After several twists and turns, this solution can solve the problem.

1. Introduce js plug-ins to jsp pages

<script type="text/javascript" src="<%=basePath%>/login.js" ></script>

2. Page initialization code

<Script type = "text/javascript"> // The input prompt information effect plug-in processes ie8 $ (function () {if (! ('Holder' in document. createElement ('input') {$ ('# password '). placeholder ({isUseSpan: true}) ;}}); </script>

3. Page tag code

<Input id = "password" name = "j_password" class = "input_text input_open" placeholder = "enter the password" type = "password">

4. Plug-in placeholder. js

/*** JQuery EnPlaceholder plug * jQuery plug-in implementing the placeholder effect across browsers * version 1.0 *} * /// var defaultValue = "Account"; (function ($) {$. fn. extend ({"placeholder": function (options) {options = $. extend ({placeholderColor: '# BABABA', // # ACA899 isUseSpan: false, // whether to use the insert span label to simulate the placeholder mode. The default value is false. The default value is onInput: true // use label simulation (isUseSpan is true), whether to bind the onInput event to replace the focus/blur event}, options); $ (this ). each (fu Nction () {var _ this = this; var supportPlaceholder = 'holder' in document. createElement ('input'); if (! SupportPlaceholder) {var defaultValue = $ (_ this). attr ('placeholder'); // when no placeholder is found, the undefined question if (defaultValue! = Null & typeof (defaultValue )! = "Undefined") {var defaultColor = optional (_this).css ('color'); if (options. isUseSpan = false) {$ (_ this ). focus (function () {var pattern = new RegExp ("^" + defaultValue + "$ | ^ $"); pattern. test ($ (_ this ). val () & amp; #95; this(.val(''0000.css ('color', defaultColor );}). blur (function () {if ($ (_ this ). val () = defaultValue) {values (_this).css ('color', defaultColor);} else if ($ (_ this ). val (). length = 0) {$ (_ this ). v Al(defavalue0000.css ('color', options. placeholderColor );}}). trigger ('blur');} else {var $ imitate =$ ('<span class = "wrap-placeholder">); restore imitate.css ({'margin-left ': ((_this).css ('margin-left'), 'margin-top': $(_this).css ('margin-top'), 'font-size': $(_this).css ('font-size '), 'font-family ': Fill (_this).css ('font-family'), 'font-westight ': Fill (_this).css ('font-westight'), 'padding-left': parseint({(_this}.css ('Padding-left') + 2 + 'pxy', 'Line-height': _ this. nodeName. toLowerCase () = 'texta '? Detail (_this).css ('line-weight '): $ (_ this ). outerHeight () + 'px ', 'padding-top': _ this. nodeName. toLowerCase () = 'textea '? Parseint(((_this).css ('padding-top ') + 2: 0}); $ (_ this ). before ($ imitate. click (function () {$ (_ this ). trigger ('focal ') ;}); $ (_ this ). val (). length! = 0 & $ imitate. hide (); if (options. onInput) {// bind oninput/onpropertychange event var inputChangeEvent = typeof (_ this. oninput) = 'object '? 'Input': 'propertychang'; $ (_ this ). bind (inputChangeEvent, function () {$ imitate [0]. style. display = $ (_ this ). val (). length! = 0? 'None': 'inline-Block';});} else {$ (_ this ). focus (function () {$ imitate. hide ();}). blur (function () {/^ $ /. test ($ (_ this ). val () & $ imitate. show () ;}}}}); return this ;}}) ;}( jQuery );

5. GAME OVER

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.