HTML代码:
CSS代码:
.auto-resize {
box-sizing: border-box;
width: 100%;
height: auto;
min-height: 30px;
padding: 5px;
resize: none;
font-size: 14px;
line-height: 1.5;
}
.tab-container {
display: flex;
}
.tab {
display: block;
border: 1px solid #ccc;
}
.tab input[type="radio"] {
display: none;
}
.tab label {
display: block;
padding: 10px;
background-color: #f2f2f2;
font-size: 16px;
}
.tab-content {
padding: 10px;
}
.tab input[type="radio"]:checked + label {
background-color: #fff;
border-bottom: 1px solid #fff;
}
JavaScript代码:
let textareas = document.querySelectorAll('.auto-resize');
function autoResize(textarea) {
textarea.style.height = 'auto';
// 计算实际内容的高度并修改textarea的高度
textarea.style.height = textarea.scrollHeight + 'px';
}
textareas.forEach(function(textarea) {
textarea.addEventListener('input', function() {
autoResize(this);
}, false);
});
// 首次自适应
textareas.forEach(function(textarea) {
autoResize(textarea);
});
在不同标签页上使用相同的类来添加文本区,并使用JavaScript代码自适应大小,将确保在不同标签页上使用文本区时自适应大小正常工作。
上一篇:不同标签上的不同悬停样式