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

Source: Internet
Author: User

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><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8"/>    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge,chrome=1"/>    <Metaname= "Renderer"content= "WebKit"/>    <Metaname= "keywords"content=""/>    <Metaname= "description"content=""/>    <title>Realization of IE browser compatible placeholder effect based on jquery</title>    <style>    *{margin:0;padding:0;}. txt{position:relative;font-size:12px;margin:10px;}. txt Input{Border:1px solid #ccc;Height:30px;Line-height:30px;padding:0 10px;width:200px;}. txt. Txt-tip{Color:#999;position:Absolute; Left:10px;Top:0;Height:32px;Line-height:34px;}    </style></Head><Body>    <Divclass= "txt">        <inputtype= "text"value=""/>    </Div></Body></HTML><Scriptsrc= "Js/jquery.min.js"></Script><Scriptsrc= "Js/placeholder.js"></Script><Script>$(function(){    var$input= $('. txt Input'); Initplaceholder ($input,'Please enter the content', 'Txt-tip');});</Script>

Placeholder.js:

functionInitplaceholder ($input, MSG, classname) {varIsie =!! Window. ActiveXObject | | ' ActiveXObject 'inchwindow; varIsplaceholder = ' placeholder 'inchDocument.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); }}functionHidephtip ($input) {var$tip = $.data ($input [0], ' tip '); if($tip) {$tip. Hide (); }}functionDealphtip ($input, $tip) {var_deal =function(){        varval =$input. Val (); if(val = = "{$tip. Show (); }Else{$tip. Hide ();    }    }; $tip. Click (function() {$input. focus ();    }); $input. On (' Input PropertyChange ',function() {cleartimeout (timeout); varTimeout = 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.