简体   繁体   English

获得选择边界的可靠,跨浏览器方式?

[英]reliable, cross-browser way to get selection's boundaries?

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. 我已经广泛阅读了SO的问题和答案,尝试了一些库,但没有运气:还没有找到可靠的工具。

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; 如何识别选择的边界并不重要:它可以是“nth textNode,mth char”,或“nth char of text”,或“nth char of html”,或者其他什么,只要它允许服务器识别文件中的要点; what really matter is that, selecting the same words of the same document must give the same result on chrome, safari, IE, firefox. 真正重要的是,选择相同文档的相同单词必须在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. 编辑:我不需要它在互联网上的任何地方工作:只在一个网站上,文件的结构是固定的,只有一个div(或类似)的内容将改变。

Try my Rangy library and its Serializer module . 试试我的Rangy库及其Serializer模块 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. 我不相信它正是你想要的,因为你提到服务器记住选择,而我的建议使用cookie,序列化选择因浏览器而异。 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. 另一方面,编写适用于所有浏览器和所有页面的内容几乎是不可能的,因为浏览器会以不同的方式解释HTML并构建不同的DOM。

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

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