用div+css实现聊天输入框,可输入、展示图片
<div class="dzm-textarea" id="dzm-textarea" contenteditable="true" @keypress="onKeypress" placeholder="输入文本" style="overflow: auto;"></div>
const onKeypress = (e)=> {
if (e.key === 'Enter') {
if (e.shiftKey) {
// Shift+Enter 按下,执行换行
initRange()
} else {
// 单纯的 Enter 按下,执行发送
handleSend()
}
e.preventDefault()
}
}
const initRange = ()=> {
const inputDiv = document.querySelector("#dzm-textarea");
inputDiv.innerText += "\n";
const lastChild = inputDiv.lastChild;
lastChild.innerText += "\n";
const selection = window.getSelection();
// 创建range对象
const range = document.createRange();
// 设置range的起始位置为div内容的第3个字符之后
range.setStart(lastChild, lastChild.length);
// 将range设置为选区
selection.removeAllRanges();
selection.addRange(range);
}
/* 输入框 */
.dzm-textarea {
flex: 1;
padding: 5px;
font-size: 14px;
max-height: 62px;
//border: 1px solid red;
}
/* 输入框为空时显示 placeholder */
.dzm-textarea:empty:before {
content: attr(placeholder);
color: rgb(200,200,200);
}
/* 输入框获取焦点时移除 placeholder */
.dzm-textarea:focus:before {
content: none;
}
标签:
div+css实现input
, input输入图片
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构