Remove placeholder after type (and not after focus)

I am using a jQuery placeholder plugin from Daniel Stocks.

This is the plugin: https://github.com/danielstocks/jQuery-Placeholder/blob/master/jquery.placeholder.js

It works actually fine for all the fields except the following little problem: In Chrome when the focus is on the field, the placeholder stays until you type something. But in IE and FF the placeholder text is removed after focus. I would them all like to behave like in Chrome.

The thing is in chrome the placeholder is natively supported, so even without the plugin it works the way I woulg like it to. In FF it works too without the plugin except with the earlier mentioned problem. And in IE it doens't work without the plugin, as you might have guessed.


I'm the author of the plugin. If you really want to disable the native support for Chrome you can edit line 71 in the code:

var NATIVE_SUPPORT = !!("placeholder" in document.createElement( "input" ));

to:

var NATIVE_SUPPORT = false

By doing this, you'll simply make ALL browsers use the plugin, even those with native support.

I'd like to point out that this is not only a "Chrome" defined behavior. Safari (and mobile safari on iOS) behaves this way. These users are most likely "used" to this behavior, so maybe ask the question, what exactly are you trying to achieve?


Replace the following section in the source from line 77:

input.focus(function() {
    placeholder.hide();
});

with this:

input.keyup(function() {
    if (input.val() != "") {
        placeholder.hide();
    }
});

I had a need for something similar and made some mods to the jquery ui placeholder plugin you mentioned.

http://jsfiddle.net/sigmabetatooth/5j55m/

feel free to bump up against it and share your thoughts.

you will also not on clearing of the field via backspace or other means the placeholder returns and on selection of the input when placeholder is present the cursor will be placed at the beginning of the text.

@Daniel if you are interested in me pushing it to you just let me know.

链接地址: http://www.djcxy.com/p/70534.html

上一篇: 删除某些输入上的IE10的“清除场”X按钮?

下一篇: 输入后(而不是焦点后)移除占位符