JavaScript-防止拖放剪切和粘贴

发布于 2025-01-26 00:42:59 字数 401 浏览 2 评论 0原文

我想防止可满足的DIV上的以下行为,因为如果DIV包含其他任何元素,则与结构混乱。

我尝试了一个明显的:

  1. 设置draggable =“ false”在div
  2. 呼叫的dressdefault上的dragstart和drop events(也在div上的事件侦听器)

上,但这似乎不起作用,我想要要定期粘贴,因此禁用这不是一个选择。

任何帮助都将不胜感激。谢谢!

I want to prevent the following behavior on a contenteditable div, because it messes with the structure if the div contains any other elements but text nodes.

enter image description here

I have tried the obvious:

  1. Setting draggable="false" on the div
  2. Calling preventDefault on the dragstart and drop events (event listeners also on the div)

However, this does not seem to work and I want to allow regular pasting, so disabling that is not an option.

Any help would be much appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2025-02-02 00:42:59

preventDefault on ondragover should prevent this behaviour.

https://www.w3schools.com/jsref/event_ondragover.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文