Created
February 16, 2013 01:18
-
-
Save kijtra/4964976 to your computer and use it in GitHub Desktop.
[JavaScript] フォームのplaceholder属性に対応していないブラウザでもそれっぽく見せる関数。要jQuery。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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