简体   繁体   English

getSelection()是否有跨浏览器的解决方案?

[英]Is there a cross-browser solution for getSelection()?

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. Javascript代码不仅要知道选定的文本 (这是微不足道的),而且还要知道anchorOffset,才能准确地知道从哪个字符中选择了文本。

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? 是否有一种可靠的方法来获取选择对象(如DOM规范中所述)?

You could try IERange , which creates a selection-like object in IE and adds a getSelection() method to window . 您可以尝试IERange ,它在IE中创建一个类似于选择的对象,并向window添加一个getSelection()方法。 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. 我一直在研究一个名为Rangy的范围/选择库,它超出了IERange提供的范围。 There's an early release available at http://code.google.com/p/rangy . http://code.google.com/p/rangy上有一个早期版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM