how to use HTML5 placeholder attribute with backward-compatibility in mind?

前端 未结 3 1112
后悔当初
后悔当初 2020-12-17 22:54

I\'d like to use HTML5\'s placeholder attribute (You can see it in cation in the newsletter at Thought Results). But when I use older browsers, of course, they don\'t render

3条回答
  •  抹茶落季
    2020-12-17 23:35

    I recommend using Modernizr to detect this feature.

    if (Modernizr.input.placeholder) {
      // your placeholder text should already be visible!
    } else {
      // no placeholder support :(
      // fall back to a scripted solution
    }
    

    If you're not interested in using this library you could use the following the code.

    function supports_input_placeholder() {
        var i = document.createElement('input');
        return 'placeholder' in i;
    }
    

    Regarding to fallback support, if you're using jQuery you could use Simple Placeholder, which had its origin here at StackOverflow.

提交回复
热议问题