使用 jquery 将某些内容附加到选定的文本
当用户右键单击所选文本时,我试图将 标记附加到所选文本中。我在堆栈中进行搜索,未找到匹配项。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
跨浏览器可靠地处理选定的文本有点棘手。 Tim Down 的库 Rangy 在那里很有用,他消除了许多浏览器特性。 (即使您不想使用该库,也可以研究它的技术。)
核心 Rangy 演示 用元素包围选定的文本,使用
RangyRange#surroundContents
方法。演示中的代码如下所示:您将执行大致相同的操作,但使用
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:You'd be doing much the same, but with an
a
rather than aspan
.编辑
注意到您谈论所选文本有点太晚了。也许你可以检查 document.elementFromPoint,我认为仅在 FireFox 中受支持。
您是否正在寻找这样的内容:
Html:
Javascript:
请参阅演示:
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:
Javascript:
See the demo:
http://jsfiddle.net/BgW7x/2/