IE9 HTML5 placeholder - how are people achieving this?

会有一股神秘感。 提交于 2019-12-01 04:47:24

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 () {
  // ...
})()();

I wrote a jquery plugin a while back that adds the placeholder support to any browser that does not support it and does nothing in those that do.

Placeholder Plugin

powerbuoy

Here's a jQuery plugin that works with password fields as well. It's not as tiny as the code suggested by Matthew but it has a few more fixes in it. I've used this successfully together with H5Validate as well.

http://webcloud.se/code/jQuery-Placeholder/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!