使用 jquery 将某些内容附加到选定的文本

发布于 2025-01-05 06:27:22 字数 81 浏览 4 评论 0原文

当用户右键单击所选文本时,我试图将 标记附加到所选文本中。我在堆栈中进行搜索,未找到匹配项。

I'm trying to append an <a> tag in to selected text when user right click on it.i searched over the stack , no matches found.

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

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

发布评论

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

评论(2

爱人如己 2025-01-12 06:27:22

跨浏览器可靠地处理选定的文本有点棘手。 Tim Down 的库 Rangy 在那里很有用,他消除了许多浏览器特性。 (即使您不想使用该库,也可以研究它的技术。)

核心 Rangy 演示 用元素包围选定的文本,使用 RangyRange#surroundContents 方法。演示中的代码如下所示:

function getFirstRange() {
    var sel = rangy.getSelection();
    return sel.rangeCount ? sel.getRangeAt(0) : null;
}
function surroundRange() {
    var range = getFirstRange();
    if (range) {
        var el = document.createElement("span");
        el.style.backgroundColor = "pink";
        try {
            range.surroundContents(el);
        } catch(ex) {
            if ((ex instanceof rangy.RangeException || Object.prototype.toString.call(ex) == "[object RangeException]") && ex.code == 1) {
                alert("Unable to surround range because range partially selects a non-text node. See DOM Level 2 Range spec for more information.\n\n" + ex);
            } else {
                alert("Unexpected errror: " + ex);
            }
        }
    }
}

您将执行大致相同的操作,但使用 a 而不是 span

Dealing with selected text reliably is a bit tricky cross-browser. Tim Down's library Rangy can be useful there, he's smoothed over a lot of the browser idiosyncrasies. (Even if you don't want to use the library, you can study it for the techniques.)

One of the core Rangy demos is surrounding selected text with an element, using the RangyRange#surroundContents method. The code for that in the demo looks like this:

function getFirstRange() {
    var sel = rangy.getSelection();
    return sel.rangeCount ? sel.getRangeAt(0) : null;
}
function surroundRange() {
    var range = getFirstRange();
    if (range) {
        var el = document.createElement("span");
        el.style.backgroundColor = "pink";
        try {
            range.surroundContents(el);
        } catch(ex) {
            if ((ex instanceof rangy.RangeException || Object.prototype.toString.call(ex) == "[object RangeException]") && ex.code == 1) {
                alert("Unable to surround range because range partially selects a non-text node. See DOM Level 2 Range spec for more information.\n\n" + ex);
            } else {
                alert("Unexpected errror: " + ex);
            }
        }
    }
}

You'd be doing much the same, but with an a rather than a span.

清音悠歌 2025-01-12 06:27:22

编辑注意到您谈论所选文本有点太晚了。

也许你可以检查 document.elementFromPoint,我认为仅在 FireFox 中受支持。

您是否正在寻找这样的内容:

Html:

<div id="rightclick">
Right Click me:   
</div>​

Javascript:

$("#rightclick").mousedown(function(e) {
    if (e.which === 3) {
        $(this).append("<a href='http://www.google.com'>Link</a>");
    }
});
​

请参阅演示:
http://jsfiddle.net/BgW7x/2/

EDIT Noticed you were talking about selected text a little too late.

Maybe you could check document.elementFromPoint, only supported in FireFox I think.

Are you looking for something like this:

Html:

<div id="rightclick">
Right Click me:   
</div>​

Javascript:

$("#rightclick").mousedown(function(e) {
    if (e.which === 3) {
        $(this).append("<a href='http://www.google.com'>Link</a>");
    }
});
​

See the demo:
http://jsfiddle.net/BgW7x/2/

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