撒科打諢
2022-07-21 20:51:29
我有點卡在看似簡單但行不通的事情上。我想在模式中創(chuàng)建一個自動調(diào)整大小的文本區(qū)域。textarea 的值根據(jù)激活模態(tài)的元素添加到模態(tài)顯示中。在模態(tài)外觀上,textarea 未調(diào)整大小,控制臺將 0 報告為 scrollHeight。如果我單擊文本區(qū)域,它會調(diào)整大小。如果我從文本區(qū)域輸入或刪除文本,它會調(diào)整大小。當(dāng)以編程方式設(shè)置值時,我無法弄清楚為什么它會報告 scrollHeight 0 。調(diào)整大小函數(shù)如下。$(document).on("input change focus", "textarea.notesarea", function (e) { this.style.height = 'auto'; console.log(this.scrollHeight+ "-"+ $(this)[0].scrollHeight); if (this.scrollHeight == 0) { this.style.height = "calc(2.25rem + 2px)"; } else { this.style.height = 0; this.style.height = (this.scrollHeight + 4) + "px"; }});
1 回答

長風(fēng)秋雁
TA貢獻1757條經(jīng)驗 獲得超7個贊
一個快速而骯臟的解決方案是強制代碼等到第一次打開模式后再添加“calc(2.25rem + 2px)”;到它的高度。您可以使用 setTimeout(function(){... 例如:
$(document).on("input change focus", "textarea.notesarea", function (e) {
this.style.height = 'auto';
console.log(this.scrollHeight+ "-"+ $(this)[0].scrollHeight);
if (this.scrollHeight == 0) {
that = this;
setTimeout(function(){
that.style.height = "calc(2.25rem + 2px)";
},300);
} else {
this.style.height = 0;
this.style.height = (this.scrollHeight + 4) + "px";
}
});
添加回答
舉報
0/150
提交
取消