Solution to invalidation of placeholder attribute in IE

2017-4-8 18:24 Originated from this website 29,260 four ten
[Abstract]

Placeholder is a very practical Html 5 attribute, but this attribute is invalid in the lower version of IE. This article gives a solution to the invalidation of the placeholder attribute in IE.

In page design, the form design needs prompt words to guide users. The most common is to use the<input>tag placeholder attribute. Unfortunately, this HTML 5 attribute will fail in the lower version of IE. What should we do?

Let's first learn about placeholder

The placeholder property provides a hint that describes the expected value of the input field. The prompt will be displayed when the input field is empty, and will disappear when the field gets the focus.

For example:

 <form action="xxx.php" method="get"> <input type="search" name="username" placeholder="username"/> </form>

When users use the form, they will see prompts such as "user name". When users click the input box, the prompt text disappears, which is very practical. However, this effect will fail in IE. If there are multiple input boxes, there is no prompt text, which is very unfriendly to users.

 Solution to invalidation of placeholder attribute in IE

You only need to use the following js code to resolve the invalidation of the placeholder attribute in IE.

 <script type="text/javascript"> ; (function($){ $.fn.placeholder = function(options){ var opts = $.extend({}, $.fn.placeholder.defaults, options); var isIE = document.all ? true : false; return this.each(function(){ var _this = this, placeholderValue =_this.getAttribute("placeholder"); // Cache the default placeholder value if(isIE){ _this.setAttribute("value",placeholderValue); _this.onfocus = function(){ $.trim(_this.value) == placeholderValue ? _ this.value = "" : ''; }; _this.onblur = function(){ $.trim(_this.value) == "" ? _ this.value = placeholderValue : ''; }; } }); }; })(jQuery); </script>

Then set the elements that need to support this function.

 <script type="text/javascript"> $("input").placeholder(); </script>

be careful: This code is based on the jquery library, so please import the jquery library file before use.

This article was last updated on September 2, 2017, and has not been updated for more than one year. If the article content or picture resources are invalid, please leave a message for feedback, and we will deal with it in a timely manner. Thank you!

If you think this article is helpful, please sponsor this website

 Alipay scanning sponsorship WeChat scanning sponsorship

  • Alipay scanning sponsorship
  • WeChat scanning sponsorship
  • Statement: All text, pictures and other materials marked "Original on this site" are copyrighted Yaxi All, welcome to reprint, but please indicate the source;
    Current comments: 4 of which: visitor 0 blogger 0
    Loading
    1. I don't understand. Come and have a look.

    2. Seven years ago (2017-04-09) 0F

      Yes, the blogger has deep research on HTML/CSS!

    Comment

     doubt  naughty  Sad  Pick one's nose  scare  smile  lovely  A bad laugh  surprised  Daze  doubt  Soldiers  Snicker  Curse  Anger  roll one's eyes  applause  proud  Wipe away sweat  kiss  Cry  open the mouth and show the teeth  halo  strong

    share four ten 29,260
    Top