可靠的跨浏览器方式来获取选择的边界?
我需要使用户能够选择一些文本,单击按钮并使服务器记住下次的选择。
我已经广泛阅读了 SO 的问题和答案,尝试了一些库,但没有运气:还没有找到可靠的工具。
如何识别选择的边界并不重要:它可以是“第 n 个文本节点,第 m 个字符”,或“文本的第 n 个字符”,或“html 的第 n 个字符”,或其他任何内容,只要它允许服务器识别文档中的要点;真正重要的是,在 chrome、safari、IE、firefox 上选择同一文档的相同单词必须给出相同的结果。
编辑:我不需要它在互联网上的任何地方工作:只在一个站点上工作,其中文档的结构是固定的,并且只有单个 div (或类似的)的内容会改变。
I need to make the user to be able to select some text, click a button and make the server remember the selection for the next time.
I've extensively read through SO's questions and answers, tried some libraries, but without luck: haven't found a reliable tool yet.
It isn't important how the selection's boundaries are identified: it could be "nth textNode, mth char", or "nth char of text", or "nth char of html", or whatever, as long as it allows the server to identify the points in the document; what really matter is that, selecting the same words of the same document must give the same result on chrome, safari, IE, firefox.
EDIT: I don't need it to work everywhere on the internet: just on one site, where the document's structure is fixed and only the content of a single div (or the like) will change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试我的 Rangy 库及其 序列化器模块。我不相信这正是您想要的,因为您提到服务器会记住选择,而我的建议使用 cookie,并且序列化选择会因浏览器而异。但是,它确实如您在第一段中所描述的那样。
另一方面,编写适用于所有浏览器和所有页面的东西几乎是不可能的,因为浏览器以不同的方式解释 HTML 并构建不同的 DOM。
Try my Rangy library and its Serializer module. I'm not convinced it's exactly what you want because you mentioned the server remembering the selection, whereas my suggestion uses cookies, and the serialized selection will vary between browsers. However, it does do as you described in the first paragraph.
On the other hand, it's pretty much impossible to write something that will work for all browsers and all pages, since browsers interpret HTML differently and build different DOMs.