有没有一种方法可以将每个文本区域自动调整为其大小's从数据库查询返回的数据
Is there a method to autofit each textarea to the size of it's data returned from a database query?
我有许多不同的查询字段数据,从数据库向多个变量返回不同的高度和长度,这些变量映射到网页上自己的文本区域以供显示。我需要一种方法让每个文本区域根据返回的大小自动调整数据。
目前,我对每个文本区域使用javascript函数(数据自动resize),以允许用户在字段大于文本区域时扩展文本:
即
$(document).ready(function () {
jQuery.each(jQuery('textarea[data-autoresize]'), function () {
var offset = this.offsetHeight - this.clientHeight;
var resizeTextarea = function (el) {
jQuery(el).css('height', 'auto').css('height', el.scrollHeight + offset);
};
jQuery(this).on('keyup input', function () {
resizeTextarea(this);
}).removeAttr('data-autoresize');
});
});
<textarea title="If needed, hit ENTER to expand this text area." class="textExpand" readOnly="true" data-autoresize rows="10"> **returnDataFieldVar1** <textarea>
这对于用户在加载数据后手动展开非常有用,并且对于10行的默认文本区域大小来说太大。但为了提高效率和功能,最好让每个文本区域自动适应查询返回的数据,并消除用户必须手动扩展网页上的文本区域的情况。任何关于如何做到这一点的指针或代码示例都将不胜感激!
你在找这样的东西吗?
$(window).on('resize', function(e) {
$('textarea[data-autoresize]').each(function() {
var $textarea = $(this);
$textarea.css('height', 'auto');
$textarea.css('height', $textarea.prop('scrollHeight') + 'px');
});
})
$('textarea[data-autoresize]').each(function() {
$(this).on('keyup input change paste propertychange', function(e) {
var $textarea = $(this);
$textarea.css('height', 'auto');
$textarea.css('height', $textarea.prop('scrollHeight') + 'px');
});
});
https://jsfiddle.net/ff0xjm4q/
在任何更改时,将高度设置为滚动高度。
由于没有文本区域调整大小事件,因此也可以在窗口调整大小时处理它。
相关文章:
- JSON数组数据返回Undefined
- AngularJS中的页面之间共享数据返回空
- post()向服务器动态生成的数据返回空响应
- 如何从jQuery对象文本正确调用服务器端ASP.NET并将数据返回给jQuery
- d3从mysql数据库导入数据返回“0”;未定义”;
- JSON可以't访问数据返回未定义
- 让 ajax 数据返回到调用函数 - 需要澄清之前的答案
- ajax 调用 PHP 数据返回 VM92:1 未捕获语法错误:意外的标记 {.
- Jquery ajax 请求无法将数据返回给其他函数
- 在 jquery 中将数据返回给 ajax 调用
- Meteor.methods:从内部回调将数据返回给客户端
- 将PHP数据返回到我当前的网页,供Javascript访问
- ajax 调用如何将数据返回给变量
- 将数据返回到索引.js节点表达式
- j查询帖子数据返回比较不起作用
- 节点 API 未将数据返回给客户端 ajax 请求
- jQuery 函数在 AJAX 数据返回上不起作用
- AJAX 将数据返回给 jquery
- 中继:获取递归数据返回空值
- 根据chrome.storage中的数据返回chrome.webRequest.onBeforeRequest的值