Skip to content

Instantly share code, notes, and snippets.

@kijtra
Created February 16, 2013 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kijtra/4964976 to your computer and use it in GitHub Desktop.
Save kijtra/4964976 to your computer and use it in GitHub Desktop.
[JavaScript] フォームのplaceholder属性に対応していないブラウザでもそれっぽく見せる関数。要jQuery。
function cross_browser_placeholder(){
var supportsInputAttribute = function (attr) {
var input = document.createElement('input');
return attr in input;
};
if (!supportsInputAttribute('placeholder')) {
$('[placeholder]').each(function () {
var input = $(this),
placeholderText = input.attr('placeholder'),
placeholderColor = '#999',
defaultColor = input.css('color');
input.focus(function () {
if (input.val() === placeholderText) {
input.val('').css('color', defaultColor);
}
})
.blur(function () {
if (input.val().replace(/[\s\t\n\r]/g,'') === '') {
input.val(placeholderText).css('color', placeholderColor);
} else if (input.val() === placeholderText) {
input.css('color', placeholderColor);
}
})
.blur()
.parents('form')
.submit(function () {
if (input.val() === placeholderText) {
input.val('');
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment