良好的口碑是企業(yè)發(fā)展的動(dòng)力
多行文本框(Multiline Textbox)是用戶界面中常見的輸入組件,允許用戶輸入或編輯多行文本內(nèi)容。以下是關(guān)于多行文本框的詳細(xì)說明和實(shí)現(xiàn)方法:
在網(wǎng)頁開發(fā)中,使用 <textarea> 標(biāo)簽創(chuàng)建多行文本框:
|
|
<textarea |
|
|
id="myTextarea" |
|
|
rows="4" |
|
|
cols="50" |
|
|
placeholder="請(qǐng)輸入內(nèi)容..." |
|
|
></textarea> |
rows:設(shè)置可見行數(shù)(高度)。cols:設(shè)置每行字符數(shù)(寬度)。placeholder:輸入框?yàn)榭諘r(shí)顯示的提示文本。name:表單提交時(shí)的字段名(如 <textarea name="content"></textarea>)。通過CSS調(diào)整文本框外觀:
|
|
#myTextarea { |
|
|
width: 100%; /* 寬度自適應(yīng)容器 */ |
|
|
height: 150px; /* 固定高度 */ |
|
|
padding: 10px; |
|
|
border: 1px solid #ccc; |
|
|
border-radius: 5px; |
|
|
resize: vertical; /* 允許用戶垂直調(diào)整大小 */ |
|
|
font-family: Arial, sans-serif; |
|
|
} |
resize:控制用戶調(diào)整大小的行為(none/both/horizontal/vertical)。box-sizing: border-box:確保padding不影響總寬度。
|
|
const textarea = document.getElementById('myTextarea'); |
|
|
textarea.addEventListener('input', () => { |
|
|
textarea.style.height = 'auto'; // 重置高度 |
|
|
textarea.style.height = textarea.scrollHeight + 'px'; // 自動(dòng)擴(kuò)展高度 |
|
|
}); |
|
|
function validateContent() { |
|
|
const content = textarea.value.trim(); |
|
|
if (content.length === 0) { |
|
|
alert("內(nèi)容不能為空!"); |
|
|
return false; |
|
|
} |
|
|
// 進(jìn)一步處理(如提交表單) |
|
|
} |
使用庫(如 Quill、TinyMCE)實(shí)現(xiàn)富文本編輯:
|
|
<!-- 示例:集成Quill編輯器 --> |
|
|
<link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet"> |
|
|
<div id="editor"></div> |
|
|
|
|
|
<script src="https://cdn.quilljs.com/1.3.7/quill.min.js"></script> |
|
|
<script> |
|
|
const quill = new Quill('#editor', { |
|
|
theme: 'snow', |
|
|
modules: { |
|
|
toolbar: [ |
|
|
['bold', 'italic', 'underline'], |
|
|
['link', 'image'] |
|
|
] |
|
|
} |
|
|
}); |
|
|
</script> |
確保正確獲取文本框內(nèi)容:
|
|
<form onsubmit="return validateContent()"> |
|
|
<textarea name="description" required></textarea> |
|
|
<button type="submit">提交</button> |
|
|
</form> |
|
|
textarea.addEventListener('input', () => { |
|
|
const maxLength = 500; |
|
|
if (textarea.value.length > maxLength) { |
|
|
textarea.value = textarea.value.slice(0, maxLength); |
|
|
} |
|
|
// 顯示剩余字符數(shù) |
|
|
document.getElementById('counter').textContent = maxLength - textarea.value.length; |
|
|
}); |
|
|
let autoSaveTimer; |
|
|
textarea.addEventListener('input', () => { |
|
|
clearTimeout(autoSaveTimer); |
|
|
autoSaveTimer = setTimeout(() => { |
|
|
localStorage.setItem('draft', textarea.value); |
|
|
}, 2000); // 每2秒自動(dòng)保存一次 |
|
|
}); |
|
|
|
|
|
// 頁面加載時(shí)讀取草稿 |
|
|
window.onload = () => { |
|
|
const draft = localStorage.getItem('draft'); |
|
|
if (draft) textarea.value = draft; |
|
|
}; |
-webkit-overflow-scrolling: touch 優(yōu)化滾動(dòng)體驗(yàn)。textContent 替代 innerHTML)。aria-label 或關(guān)聯(lián) <label> 標(biāo)簽。根據(jù)具體需求選擇原生實(shí)現(xiàn)或第三方庫(如React的react-textarea-autosize),可快速提升開發(fā)效率。
全天候在線客服支持
10年互聯(lián)網(wǎng)服務(wù)經(jīng)驗(yàn)
全國300余家服務(wù)機(jī)構(gòu)
1000余家合作企業(yè)
友情鏈接:東莞網(wǎng)站建設(shè)公司 東莞企業(yè)網(wǎng)站模板 zblog模板 菏澤網(wǎng)站建設(shè) 淮安網(wǎng)站建設(shè) 廣州網(wǎng)站建設(shè) 能耗監(jiān)測系統(tǒng) 無錫網(wǎng)絡(luò)推廣 外貿(mào)網(wǎng)站建設(shè) 東莞獵頭公司
聲明:本站部分素材來自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)立即聯(lián)系刪除。
企信東莞網(wǎng)站建設(shè)公司讓你低成本做網(wǎng)站,現(xiàn)在聯(lián)系可贈(zèng)送基礎(chǔ)seo關(guān)鍵詞排名服務(wù)。東莞做網(wǎng)站找企信。