我在jsp页面中使用auto grow-able textarea以格式化方式显示输出。我希望它的大小能够适应要显示的内容的高度。但它似乎没有用。这是我的jsp: -
<body onload="init();">
<div id="mainDivContainer">
<div id="insideMainDivContainer">
<a href="InputTest02.html">Back</a>
<div>
<table align="center">
<thead>
<tr>
<th>Text</th>
</tr>
</thead>
<%
List<TextBean> list = TextDAO.getData();
for (TextBean bean : list) {
%>
<br/>
<tbody>
<tr>
<textarea id="text" name="qa" form="form" rows="10" cols="60"><%=bean.getText() %></textarea>
</tr>
</tbody>
<%
}
%>
</table></div>
</div>
</div>
</body>
这是可增长文本区域的javascript: -
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
}
有两个问题: -
答案 0 :(得分:0)
只需将这一系列CSS样式添加到textarea:
textarea { resize: both; }
如果您想限制resizeablity的方向,只需编辑resize
属性:
textarea { resize: vertical; }
textarea { resize: horizontal; }