gpt4 book ai didi

javascript - HTML 中的空文本

转载 作者:太空宇宙 更新时间:2023-11-04 15:17:42 24 4
gpt4 key购买 nike

有没有办法像 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com