そのため、プレースホルダー属性をIE9以下で機能させようとしています。ただし、奇妙な動作が発生しています (以下のスクリーンショットを参照)。
このウェブサイトを見つけて、以下の JS コードを使用することにしました。
JS:
// Checks browser support for the placeholder attribute
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// Placeholder for IE
$(function () {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text, textarea').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text, textarea').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
}
});
HTML:
<p>
<label>To: </label>
<input type="text" id="toEmail" placeholder="Recipient's Email Address" />
</p>
<p>
<label>From:</label>
<input type="text" id="fromName" placeholder="Your Name" />
</p>
<p>
<label>From: </label>
<input type="text" id="fromEmail" placeholder="Your Email Address" />
</p>
<p>
<label>Message:</label>
<textarea rows="5" cols="5" id="messageEmail"></textarea>
</p>
CSS:
.hasPlaceholder {
color: #999;
}
私のウェブサイトは、フォームを含むモーダルを開きます。ただし、モーダルを最初に開くと、プレースホルダー テキストは表示されません。
ただし、テキスト フィールドをクリックしてからテキスト フィールドの外をクリックすると、プレースホルダー テキストが表示されます。
モーダルを開いた直後にテキストが表示されるようにしたいと思います。それだけです 本当に...
参考までに、モーダルが最初に非表示になっているため、テキストが最初に表示されない可能性があると思います。その場合、どうすれば修正できますか?
注:プラグインが存在することは知っています。プラグインを使用したくありません。