有getSelection()的跨浏览器解决方案吗? [英] Is there a cross-browser solution for getSelection()?

查看:146
本文介绍了有getSelection()的跨浏览器解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个评论机制,其中用户突出显示一段文字,点击评论这个,然后做一些事情。 Javascript代码必须知道不仅选择的文本(这很简单),还要知道anchorOffset,以准确知道从哪个字符选择文本。



我找到了一个跨浏览器解决方案,为您提供文本。是否有可靠的方法来获取选择对象(如DOM规范中所述)?

解决方案

您可以尝试 IERange ,它在IE中创建一个类似于选择的对象,并向窗口 getSelection() c $ c>。



更新 p>

我一直在使用Rangy的范围/选择库,这超出了IERange提供的范围。有一个早期版本可以在 http://code.google.com/p/rangy


I need to make a comment mechanism in which user highlights a piece of text, clicks "comment this", and then does something. The Javascript code has to know not only the selected text (this is trivial), but also the anchorOffset, to know exactly from which to which character the text was selected.

I've found a cross-browser solution that gives you the text. Is there a reliable way to get a selection object as it's described in DOM specs?

解决方案

You could try IERange, which creates a selection-like object in IE and adds a getSelection() method to window. I don't think it's perfect but it's about the best standalone Range/selection library there is that I've seen.

Update

I've been working on a Range/selection library called Rangy that goes beyond what IERange provides. There's an early release available at http://code.google.com/p/rangy.

这篇关于有getSelection()的跨浏览器解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆