JQuery placeholder plugin allows IE to also support placeholder properties

Source: Internet
Author: User

JQuery placeholder plugin allows IE to also support placeholder properties

Case: whole search box, need default placeholder for "Please enter keyword", get focus, placeholder disappears or unavailable (does not affect normal input), after losing focus, if the user has no content input, placeholder continues to appear, continue to occupy. This code I think the front end has been very easy to write it, now HTML5 original has this "placeholder" attribute, the effect is the same as the above case description (each supported browser may not be consistent within the performance, but the role is consistent), So how does this attribute gracefully downgrade to support all modern browsers? The answer or script is JavaScript.

;(function($) {$.fn.extend ({placeholder:function () {            if("Placeholder"inchDocument.createelement ("Input")) {                return  This //If native supports the placeholder property, the object itself is returned}Else {                return  This. each (function () {                    var_this = $ ( This); _this.val (_this.attr ("placeholder")). Focus (function () {                        if(_this.val () = = = _this.attr ("placeholder") {_this.val ("")}). blur (function () {                        if(_this.val (). length = = 0) {_this.val (_this.attr ("Placeholder"))}})}) (jQuery);
script to determine whether the browser natively supports the placeholder property to dynamically load this script
if  in document.createelement ("input")) {    $.getscript ("jquery.placeholder.js",function  () {        $ ("#keyword"). placeholder ();   let Id=keyword elements support placeholder, switch to your own selector     })}

JQuery placeholder plugin allows IE to also support placeholder properties

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.