ewe*_*ein 0 javascript jquery html5
我的应用程序需要文本字段和textarea字段的占位符.我知道Internet Explorer不支持占位符.我环顾四周,发现了一些只适用于文本字段的后备代码.我怎么能让这个也适用于textarea.
码:
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$('input[type=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');
}
});
$('input[type="text"], textarea').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<li>
<input type="text" placeholder="To:" id="to" autocapitalize="off" autocorrect="off" autocomplete="off" />
</li>
<li>
<textarea placeholder="Message:" id="body" rows="10" cols="30" autocapitalize="off" autocorrect="off" autocomplete="off"></textarea>
</li>
Run Code Online (Sandbox Code Playgroud)
更改$(':text')到$('input[type="text"], textarea')
或者,这个现有的jQuery插件:
https://github.com/mathiasbynens/jquery-placeholder
适用于textareas和许多类型的输入字段,包括密码字段
| 归档时间: |
|
| 查看次数: |
7133 次 |
| 最近记录: |