html - 光标位置不在 Microsoft Edge 中 contentEditable div 中占位符的开头
问题描述
可竞争 div 上的光标不是占位符的开始,而它在 chrome 和 firefox 中工作正常。
div 是自动聚焦的,光标必须从初始位置开始显示。
html
<div id="" class="chatInput" contenteditable="true" placeholder="Type something..." autofocus="autofocus"></div>
css
.cInput{
padding: 5px 15px;
color: var(--text-base-volumeone);
outline: none;
cursor: text;
max-height: 32px;
overflow-x: hidden;
overflow-y: auto;
width: 100%;
min-height: 32px;
font-family: LATORegular;
font-size: 13px;
vertical-align: bottom;
transition: border-color 0.25s ease;
caret-color: var(--caret-color);
position: relative;
white-space: initial;
}
解决方案
我尝试搜索该问题,它看起来像是 MS Edge 旧版浏览器中的一个已知问题。
这是您可以使用 MS Edge 旧版浏览器尝试的解决方法。
<!DOCTYPE html>
<html>
<head>
<style>
[contenteditable=true] {
position: relative;
}
[contenteditable=true]:empty:before {
content: attr(placeholder);
position: absolute;
left: 0;
right: 0;
top: 4px;
margin: auto;
}
#myDiv {
border: 1px dashed #AAA;
width: 290px;
padding: 5px;
text-align: left;
height: 16px;
}
</style>
</head>
<body>
<div id="myDiv" contenteditable="true" placeholder="Enter text here..."></div>
</body>
</html>
MS Edge 旧版浏览器中的输出:
此解决方案使用 CSS 代码。我们可以注意到光标在 div 之外几乎没有显示。如果这不是可取的,那么您可能需要使用 JavaScript 或 Jquery 将光标显示在起始位置。
此示例使用 JQuery。
推荐阅读
- c# - UWP NavigationView 防止卸载页面
- ionic-framework - 如何在 ionic 3 pwa 应用程序中禁用 Android 后退按钮
- reactjs - 反应从 DOM 计算的初始状态
- asp.net-mvc - 将 Visual Studio 2017 从移动开发更改回 Web 开发
- node.js - 为什么 Angular CLI 不能识别当前的 Node.js 版本?
- javascript - TypeError:TypeError:null 不是对象(评估“this.state.email”)
- react-native - 在减速器中无法将所有 api 数据放在一起
- htdocs - 如何使用 php 保护 htdocs 文件?
- bash - Bash 命令查找与 file[0-9]+.js 匹配的文件
- javascript - 为什么 Jquery $(#); 不起作用,但 Javascript document.getElementById 可以正常工作吗?