gpt4 book ai didi

dart - 如何在 dart 中自动增长文本区域元素?

转载 作者:行者123 更新时间:2023-12-02 00:10:39 26 4
gpt4 key购买 nike

我想要一个 textarea 元素,可以根据 textarea 元素的内容动态调整其高度。

我如何在 Dart 中实现这个?

最佳答案

仅使用纯 dart:html:

var textArea = querySelector('textarea');
textArea.onInput.listen((_) {
// shrink the textarea when needed
textArea.style.height = 'auto';

// set the height to scrollHeight plus some correction
var correction = textArea.offsetHeight - textArea.clientHeight;
textArea.style.height = '${textArea.scrollHeight - correction}px';
});

您还可以创建一个 angular2 指令:

@Directive(
selector: 'textarea[autogrow]',
host: const {
'(input)': 'onInput(\$event.target)'
}
)
class AutogrowDirective {

onInput(TextAreaElement textArea) {
// shrink the textarea when needed
textArea.style.height = 'auto';

// set the height to scrollHeight plus some correction
var correction = textArea.offsetHeight - textArea.clientHeight;
textArea.style.height = '${textArea.scrollHeight - correction}px';
}
}

关于dart - 如何在 dart 中自动增长文本区域元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269718/

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