Javascript 在 ContentEditable Div 中设置插入符

发布于 2024-09-16 13:37:14 字数 341 浏览 4 评论 0原文

我有一个内容可编辑的 div 标签,以便用户可以在 div 中键入内容。有一个函数可以在用户按下按钮时将链接添加到 div 中。我希望插入符号位于链接之后,以便用户可以继续键入。该链接可以插入多次。

示例代码:

<div id="mydiv" contenteditable="true" tabindex="-1">before <a href="#">link</a> after</div>

我需要此代码在以下版本中工作:IE、Firefox、Opera、Safari 和 Chrome。

有人可以提供任何帮助吗?

I have a div tag that is contenteditable so that users can type in the div. There is a function that adds a link into the div when the user presses a button. I would like the caret to be positioned after the link so that users can continue to type. The link can be inserted many times.

Example Code:

<div id="mydiv" contenteditable="true" tabindex="-1">before <a href="#">link</a> after</div>

I require this code to work in: IE, Firefox, Opera, Safari and Chrome.

Can anyone offer any help?

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

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

发布评论

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

评论(2

感受沵的脚步 2024-09-23 13:37:14

假设您有对已插入名为 link 的变量中的 元素的引用:

function setCaretAfter(el) {
    var sel, range;
    if (window.getSelection && document.createRange) {
        range = document.createRange();
        range.setStartAfter(el);
        range.collapse(true);
        sel = window.getSelection(); 
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(el);
        range.collapse(false);
        range.select();
    }
}

setCaretAfter(link);

Assuming you have a reference to the <a> element you've inserted in a variable called link:

function setCaretAfter(el) {
    var sel, range;
    if (window.getSelection && document.createRange) {
        range = document.createRange();
        range.setStartAfter(el);
        range.collapse(true);
        sel = window.getSelection(); 
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(el);
        range.collapse(false);
        range.select();
    }
}

setCaretAfter(link);
橘和柠 2024-09-23 13:37:14

我查看了 CKEditor (http://ckeditor.com/) 代码,发现它有一个appendBogus() 函数,并插入了一个额外的
 然后将其删除。

当然,问题在于 Gecko/IE 浏览器在如何
上也存在细微差别。标签也可以工作(即是否使用 \r 或 \n 插入范围与添加
元素)

您可以通读 _source/plugins/enterkey/plugin.js 以查看处理 IE 的不同细微差别&壁虎。这似乎是一个大黑客......

I looked inside the CKEditor (http://ckeditor.com/) code and found that it has an appendBogus() function as well as inserts an extra <br><span> </span></br> that is then deleted.

The problem of course is that Gecko/IE browsers also have nuances about how <br> tags work too (i.e. whether to use \r or \n to insert into the range vs. adding an <br> element)

You can read through the _source/plugins/enterkey/plugin.js to see the different nuances with handling IE & Gecko. It seems like one big hack...

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