入力フォームにテキストをグレー表示できるようにするコードをいくつか書きました。ユーザーがそれにフォーカスすると、テキストが消えてグレー表示されなくなります。
function editableAlt (elem, colour1, colour2) {
var elem = document.getElementById(elem);
elem.onfocus = function () {
if(this.value == this.defaultValue) {
this.value = ""; // remove the default text, so the user can enter their own
this.style.color = "#" + colour1; // change the text colour
}
};
elem.onblur = function () {
if(this.value == '') {
this.value = this.defaultValue; // user left it blank? revert back to the default text
this.style.color = "#" + colour2; // and change the colour back too
}
}
}
これはほとんどのページで機能しますが、何らかの理由で onfocus と onblur がまったく機能しないページがあります。たとえば、onclick に変更しても問題は発生しません。
他に試せることはありますか?:(私はJQueryを使用していますが、それを削除してもまったく影響がないようです。
乾杯