"jquery" based on jquery for IE browser compatible placeholder effect

Source: Internet
Author: User

Original link: http://www.cnblogs.com/yjzhu/p/4398835.html

Placeholder is a newly added property of HTML5 that provides a hint (hint) that describes the values expected by the input fields. The hint appears when the input field is empty and disappears when the field gets focus. The placeholder property applies to the following types of input tags: text, search, URL, telephone, email, and password.

Let's look at the effect in Google Chrome first:

When you get focus:

Input field:

Because it is the HTML5 attribute, the natural low version of the browser such as IE6-8 is incompatible. Here is how to show the above effect in the lower version of the browser, not much to say directly on the code.

Html:

<!doctype html>

Placeholder.js:

function Initplaceholder ($input, MSG, classname) {var Isie =!! Window. ActiveXObject | |    ' ActiveXObject ' in window;    var isplaceholder = ' placeholder ' in document.createelement (' input ');    if (Isplaceholder &&!isie) {$input. attr (' placeholder ', msg);        }else{var $tip = $ (' <span></span> ');        $input. Removeattr (' placeholder ');        if ($input. Is (': Hidden ')) {$tip. Hide ();        } $tip. addclass (classname). Text (msg);        $input. After ($tip);        $.data ($input [0], ' tip ', $tip);        if ($input. val () = ") {Hidephtip ($input);    } dealphtip ($input, $tip);    }}function Hidephtip ($input) {var $tip = $.data ($input [0], ' tip ');    if ($tip) {$tip. Hide ();        }}function Dealphtip ($input, $tip) {var _deal = function () {var val = $input. val ();        if (val = = ") {$tip. Show ();        }else{$tip. Hide ();    }    };   $tip. Click (function () {$input. focus (); });        $input. On (' Input PropertyChange ', function () {cleartimeout (timeout);    var timeout = setTimeout (_deal, 0); });}

The principle of implementation: first filter the browser, non-IE browser and support placeholder properties with Placeholder,ie browser then use span instead of placeholder text content, through the style positioning on input, while listening input box value Changes to determine whether to show or hide span.

Let's look at the effect in the IE6 browser:

When you get focus:

Input field:

Can see and Google Browser effect is consistent, the only problem is that the text is not centered, can be modified through the CSS.

"jquery" based on jquery for IE browser compatible placeholder effect

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.