IE9 HTML5 placeholder - how are people achieving this?

前端 未结 3 1499
一个人的身影
一个人的身影 2021-01-12 21:59

I\'m trying to use the placeholder=\"xxx\" attribute in my web application, and I don\'t want to have a special visual for IE9. Can people throw out some good s

3条回答
  •  清酒与你
    2021-01-12 22:27

    We just researched the same thing. We decided on reusing this gist, by Aaron McCall, after making some minor changes. The main advantage is that it's simple, easy to understand code:

    1. Remove the kernel and setup_placeholders parts. Just call it immediately in an anonymous function.

    2. Add var before test.

    For browsers that support placeholder, it simply falls back to that. It also handles new input elements (note the use of delegate) in existing forms. But does not handle dynamic new form elements. It could probably be modified to do so with jQuery.on.

    If you don't like this one, you can use one of the ones here. However, some of them are overcomplicated, or have questionable design decisions like setTimeout for detecting new elements.

    Note that it needs to use two pairs of parens, since you're calling an anonymous function, then calling the returned function (this could be factored out differently):

    (function () {
      // ...
    })()();
    

提交回复
热议问题