在内容可编辑中模拟拖动类型文本/纯文本行为

Emulate drag type text/plain behavior in contenteditable

本文关键字:文本 类型 拖动 模拟 编辑      更新时间:2023-09-26

如果您拖动例如文本/纯文本或文本/html,则要放置的内容可编辑元素会将插入符号/光标放在放置拖动内容的位置。

一个非常基本的小提琴来说明我的意思:示例

<div contenteditable="true">DROP TEXT HERE</div>
This text wants to be dropped.

如何模拟自定义数据类型的此行为?或者:悬停时如何始终将插入符号放在鼠标指针旁边?

你可以使用 CSS :empty 伪选择器

[contenteditable="true"] {
    border: 2px solid #ccc;
    min-height: 1em;
}
[contenteditable="true"]:empty:before {
    content: 'DROP TEXT HERE';
    color: #999;
}