jQuery - 获取选择的段落、长度等
我正在尝试完成一些我认为在 jQuery 中应该很容易完成的事情。
我的网站上有很长的文字。 每当用户选择(突出显示)文本时,我需要获取以下信息:
- 选择开始的段落
- 选择开始的段落中的字符
- 选择的长度
- 选择结束的段落(如果突出显示超出了一个段落)
- 选择结束的该段落中的字符,
谢谢;)
I'm trying to accomplish something I believe should be simple to do in jQuery.
I have a long text in my website.
Whenever a user selects (highlights) a text, I need to get the following info:
- The paragraph where the selection begins at
- The char in that paragraph that the selection starts at
- The length of the selection
- The paragraph where the selection ends at (in case the highlight goes beyond one paragraph)
- The char in that paragraph where the selection ends
Thanks ahead ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我之前已经在 Stack Overflow 上回答过所有这些问题。依次:
getAncestor()
和的组合getSelectionBoundaryContainerElement() 来自 此处将执行以下操作这
您需要对 API 进行一些研究才能做到这一点,这并不是坏事。
I think I've answered all of these on Stack Overflow before. In turn:
getAncestor()
from here andgetSelectionBoundaryContainerElement()
from here will do the jobYou'll need to do a little study of the APIs to do this, which is no bad thing.
您需要使用 JavaScript 来处理任何涉及文本选择的事情。 jQuery 没有内置函数来检测选择的开始或结束位置。 Rangy http://code.google.com/p/rangy/ 可能是您可以做的事情用于此类目的。
You need to use JavaScript for anything that deals with text selections. jQuery has no built in function for detecting where a selection begins or where it ends. Rangy http://code.google.com/p/rangy/ could be something you can use for such purpose.
查看
此网站上的示例:http://www.codetoad.com/javascript_get_selected_text.asp
play around with
See an example on this site: http://www.codetoad.com/javascript_get_selected_text.asp