删除 contentEditable div 上的调整大小处理程序

发布于 2024-08-14 18:10:14 字数 215 浏览 7 评论 0原文

我创建了一个 contentEditable div 用作富文本区域。它周围有我想摆脱的调整大小处理程序。知道我该怎么做吗?

编辑:这似乎正在发生,因为我绝对定位了 div,因此 Firefox 向元素添加了一个令人恼火的 _moz_resize 属性,我无法关闭该属性。

替代文本

I created a contentEditable div to use as a rich textarea. It has resize handlers around it that I'd like to get rid of. Any idea how I'd do this?

Edit: This appears to be happening because I am absolutely positioning the div, so Firefox adds an infuriating _moz_resize attribute to the element which I cannot turn off.

alt text

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

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

发布评论

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

评论(7

瞎闹 2024-08-21 18:10:14

顺便说一句,您可以通过向文档发送(记录有些少的)enableObjectResizing 命令来禁用 Firefox 的自动调整大小处理功能:

document.execCommand("enableObjectResizing", false, false);

AFAIK,只有在文档加载后才能安全地执行此操作,据我所知,没有办法禁用抓取器,这是一个单独的功能。

Just as a side note, you can disable Firefox's automatic resize handle feature by sending the (somewhat poorly-documented) enableObjectResizing command to the document:

document.execCommand("enableObjectResizing", false, false);

AFAIK, this can only safely be done once the document has loaded, and there's no way I know of to disable the grabber, which is a separate feature.

深爱不及久伴 2024-08-21 18:10:14

看来我可以通过添加包装器 div 并绝对定位包装器然后使内部 div 内容可编辑来解决此问题。

It looks like I'll be able to work around this by adding a wrapper div and absolutely positioning the wrapper and then making the inner div contentEditable.

染墨丶若流云 2024-08-21 18:10:14

在 Chrome 39 中,这些句柄似乎不存在,即使您希望它们存在。

在 Firefox 中,可以简单地使用 execCommand,就像 ZoogieZork 回答的那样。

但在 Internet Explorer 中,此功能无法关闭。必须解决这个问题。

WYMeditor 开发中,这是我发现的。

结果如下:

  • 在 IE 中,调整大小 UI 会出现一瞬间,然后消失。用户似乎没有办法使用它。
  • 图像是通过 mouseup 选择的文本
  • 能够拖动图像。在某些浏览器中,可能必须在拖动之前选择它们。正如前一项所述,简单的鼠标移动将导致选择图像。
  • 图像是使用文本选择而不是“控件选择”(提供调整大小 UI)来选择的。

这是我经过几个小时的深呼吸后能想到的最好的办法。我认为如果你真的想摆脱这些手柄就足够了。

在 IE 中,设置 oncontrolselect 在图像上返回 false 确实可以防止这些句柄出现,并且您可以通过将以下处理程序附加到 来巧妙地做到这一点>mousedown 事件:

function (evt) {
    var img;

    function returnFalse() {
        return false;
    }

    if (evt.tagName.toLowerCase() === "img") {
        img = evt.target;
        img.oncontrolselect = returnFalse;
    }
}

它实际上不能完全正常工作。它工作得不太好的原因是,为了开始对图像进行拖放操作,必须按住鼠标而不移动它一瞬间,然后才开始移动它一段时间。阻力。如果按下鼠标并立即开始拖动,图像将保留在其位置并且不会被拖动。

所以我没有那么做。

我所做的如下。在所有浏览器中,我都使用 mouseup 来专门通过文本选择目标图像。在非 IE 和 IE11 中,同步:

function (evt) {
    if (evt.target.tagName.toLowerCase() === "img") {
        selectSingleNode(img); // In my case, I used Rangy
    }
}

在 IE 7 到 10 中,异步:

function (evt) {
    if (evt.target.tagName.toLowerCase() !== "img") {
        return;
    }

    window.setTimeout(function () {
        selectSingleNode(img); // In my case, I used Rangy
    }, 0);
}

这确保了这些句柄出现后,它们会尽快消失,因为图像会丢失其“控件选择”,因为该选择被替换为常规文本选择。

在 Internet Explorer 7 到 11 中,我将一个处理程序附加到 dragend 来删除所有选择:

function (evt) {
    if (evt.target.tagName.toLowerCase() === "img") {
        deselect(); // I use Rangy for this, as well
    }
}

这使得拖放后显示的手柄消失。

我希望这对您有所帮助,并且希望您能做得更好。

In Chrome 39, these handles don't seem to exist, even if you wanted them to.

In Firefox, one can simply use execCommand, like ZoogieZork answered.

But in Internet Explorer this can't be turned off. It must be worked around.

In WYMeditor development, here's what I've found.

The following results in:

  • In IE, the resize UI shows up for a split second and then disappears. There seems to be no way for the user to use it.
  • Images are text selected on mouseup
  • Ability to drag images. In some browsers, they may have to be selected before dragging. As written in the previous item, a simple mouseup will result in an image being selected.
  • Images are selected using text selection and not "control selection" (that which provides the resize UI).

This is the best I could come up with after hours of very deep breaths. I think it is good enough if you really want to get rid of those handles.

In IE, Setting oncontrolselect to return false on the image, really does prevent those handles from appearing, and you can do it cleverly, by attaching the following handler to the mousedown event:

function (evt) {
    var img;

    function returnFalse() {
        return false;
    }

    if (evt.tagName.toLowerCase() === "img") {
        img = evt.target;
        img.oncontrolselect = returnFalse;
    }
}

It actually doesn't work completely well. The reason that it didn't work very well is that in order to begin a drag and drop operation on the image, one had to press and hold the mouse, without moving it, for a split second, and only then begin moving it for the drag. If one pressed the mouse and immediately began dragging, the image would remain in its place and not be dragged.

So I didn't do that.

What I did is the following. In all browsers, I used mouseup to text select the target image exclusively. In non-IE and IE11, synchronously:

function (evt) {
    if (evt.target.tagName.toLowerCase() === "img") {
        selectSingleNode(img); // In my case, I used Rangy
    }
}

In IE 7 through 10, asynchronously:

function (evt) {
    if (evt.target.tagName.toLowerCase() !== "img") {
        return;
    }

    window.setTimeout(function () {
        selectSingleNode(img); // In my case, I used Rangy
    }, 0);
}

This made sure that after those handles show up, they disappear ASAP, because the image loses its "control selection" because that selection is replaced with a regular text selection.

In Internet Explorer 7 through 11, I attached a handler to dragend that removes all selection:

function (evt) {
    if (evt.target.tagName.toLowerCase() === "img") {
        deselect(); // I use Rangy for this, as well
    }
}

This makes the handles that show up after drag and drop, disappear.

I hope this helps and I hope you can make it even better.

赠意 2024-08-21 18:10:14

我只是面临这个问题。
我尝试了 document.execCommand("enableObjectResizing", false, false); 但是,移动图标仍然出现。当 onmousedown 事件发生时,解决我的问题的只是 e.preventDefault()

element.onmousedown = function(e){
       e.preventDefault();
}

I just face that problem.
I tried document.execCommand("enableObjectResizing", false, false); but, the move icon was still appearing. What just fix my problem was just e.preventDefault() when onmousedown event occurs.

element.onmousedown = function(e){
       e.preventDefault();
}
來不及說愛妳 2024-08-21 18:10:14

对于 IE11(我没有测试过旧版本的 IE,但我觉得它会起作用),您可以将 contenteditable="false" 属性添加到 img 标签。这将防止在保持拖放到位的同时进行任何大小调整。

for IE11 (I havn't tested the older versions of IE, but I feel like it would work) you can add contenteditable="false" attribute to the img tag. This will prevent any re-sizing from being done while keeping drag and drop in place.

居里长安 2024-08-21 18:10:14

...只是有史以来最好的修复

<div contenteditable="true">
    <label contenteditable="false"><input/></label>
</div>

或包装您的 input/img 的任何 html 元素

像魅力一样在 IE11 上工作

... just the best fix ever

<div contenteditable="true">
    <label contenteditable="false"><input/></label>
</div>

or any html element that wraps your input/img

Works on IE11 like a charm

娇纵 2024-08-21 18:10:14

您是否尝试过将样式添加

border: none;

到 div 中?

Have you tried adding the style

border: none;

to the div?

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