有没有办法像 ExtJS 那样在输入字段中添加类似于 emptyText 的内容?
我尝试使用更改后的 CSS 设置值。但是,该值是随表单一起提交的,并且在我单击输入字段后它并没有消失。我需要支持IE7及以上版本
如有任何帮助,我们将不胜感激。
最佳答案
你要找的是placeholder
.. W3School
<input type="text" placeholder="Hii" />
您可以为 ie 和其他不支持占位符的旧版本浏览器找到 polyfills..
您可以为不支持占位符的浏览器添加此代码,以使其在良好的浏览器中以相同的方式工作..*它需要 JQuery
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// This adds placeholder support to browsers that wouldn't otherwise support it.
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).attr('placeholder') != undefined && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && $(this).attr('placeholder') != undefined && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$(':text.hasPlaceholder').val('');
});
}
});
关于javascript - HTML 中的空文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257430/