【问题标题】:How to make a readonly <s:textarea /> fit to its content如何使只读 <s:textarea /> 适合其内容
【发布时间】:2014-09-26 15:16:51
【问题描述】:

我的表单中有一个文本区域:

<s:textarea key = "appInfo.collection" 
       required = "true" 
       readonly = "true"
       cssClass = "form-control" 
           rows = "8"/>

还有几个文本区域字段。我想让这些文本区域在只读模式下适合它们的内容。

由于我指定了rows=8,如果内容仅填充 2 行,它有时会显示一个空白区域,或者如果内容超过 8 行,它会显示一个滚动条。

如何根据内容为这些文本区域提供正确的大小,而不使用滚动条?

【问题讨论】:

标签: javascript html struts2 textarea struts-tags


【解决方案1】:

您可以使用我评论中链接的the answer

关于你的评论

我的文本区域处于阅读模式,我没有可供它们使用的 keyup 功能

您不需要任何keyup 处理程序:因为它是只读的,所以在页面加载后执行一次:

<s:textarea id = "myTextarea"
           key = "appInfo.collection" 
      required = "true" 
      readonly = "true"
      cssClass = "form-control" 
          rows = "8"/>
function autoheight(a) {
    if (!$(a).prop('scrollTop')) {
        do {
            var b = $(a).prop('scrollHeight');
            var h = $(a).height();
            $(a).height(h - 5);
        }
        while (b && (b != $(a).prop('scrollHeight')));
    };
    $(a).height($(a).prop('scrollHeight') + 20);
}

$(function(){ 
    // call the function in document.ready 
    autoheight($("#myTextarea"));
});

【讨论】:

  • 谢谢,我以同样的方式完成了它,只是我忘记了在 document.ready() 函数上调用它。我在
  • 不客气。我不确定你的其他问题,但我记得我在这样的 SO 脚本上看到过 maxHeight 处理(用于在大尺寸文本区域上添加滚动条),而对于最小高度,它可能与一些默认值有关,并且我也尝试更改 rows 属性
  • 我确实已经更改了 rows 属性。它工作得很好。想想只需将值 20 更改为更小的值,以在底部添加更少的空间。再次感谢对我有用。
猜你喜欢
  • 1970-01-01
  • 2020-07-25
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 2017-02-17
  • 2023-03-25
相关资源
最近更新 更多