javascript - HTML 中的空文本

标签 javascript html extjs

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

相关文章:

javascript - d3.js:如何将标签添加到图表上的散点

html - 使用图像 Sprite 在 html 按钮中显示悬停和事件状态

javascript - 浏览器自动填充选项与 Angular Material 自动完成选项重叠

javascript - 如何将商店注入(inject)面板的工具栏中?

javascript - 使用 hasOwnProperty 的合适/推荐方法是什么?

JavaScript 库通过点击导航菜单触发

javascript - 使用第一个选项自动提交表单 (PHP)

javascript - clearInterval 不重置

javascript - ExtJS 4 存储中少数实例中的唯一记录

layout - 如何在 ExtJS 4 中实现流程布局?