jQuery-获取选择的段落,长度等 [英] jQuery - get selection's paragraph, length, etc

查看:163
本文介绍了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:

  • 选择开始的段落
  • 该段中选择的起始字符
  • 选择的长度
  • 选择结束的段落(以防突出显示超出一个段落)
  • 选择结束的那个段落中的字符

谢谢;)

推荐答案

我想我已经在Stack Overflow上回答了所有这些问题.反过来:

I think I've answered all of these on Stack Overflow before. In turn:

  • 此处getAncestor()getSelectionBoundaryContainerElement()的组合://stackoverflow.com/questions/5442091/how-do-i-know-which-element-is-modifying-in-contenteditable-case/5446726#5446726>此处将完成这项工作
  • 这取决于您所说的选择开始于该段中的字符"的意思.如果该段落内部可能包含其他元素,而不仅仅是一个文本节点,那么这将变得有些棘手.您可以修改.
  • 同样,这取决于您所说的选择的长度".您可以获取选择文本作为字符串并使用其length属性.
  • 查看选择答案的答案
  • 查看选择答案的答案
  • A combination of getAncestor() from here and getSelectionBoundaryContainerElement() from here will do the job
  • It depends what you mean by "char in that paragraph that the selection starts at". If the paragraph may have other elements inside rather than just one text node, this becomes slightly tricky. You could adapt this.
  • Again, this depends on what you mean by the "length of the selection". You could get the selected text as a string and use its length property.
  • See answers for selection start
  • See answers for selection start

您需要对API进行一些研究才能做到这一点,这不是一件坏事.

You'll need to do a little study of the APIs to do this, which is no bad thing.

这篇关于jQuery-获取选择的段落,长度等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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