如何通过突出显示的位置获取该行的文本

时间:2014-02-25 07:59:18

标签: javascript jquery html

我有一个包含长段文本的div。我还有一条滚动线,可以向上或向下移动。 我想得到它当前所在的文本,比如在屏幕下方,你可以看到该行的位置在第3行。基本上突出显示的行是具有绝对位置的div。请建议我如何获得突出显示的文本。

由于

这是该段的屏幕截图

enter image description here

实际上突出显示的行是可移动的选择彩色div可以用鼠标向上或向下移动,我想得到段落的哪一行的文本。

有一个像这个结构的表

<table>
<tr>
<td>
    <!-- here is the pragraph with many lines -->

    <div>In 20 years our technology will reach a level of personalization that will enhance every moment of our lives. We’ll be more physically comfortable with the furniture we sit on and the products we hold; only the most relevant and personalized information from friends and family will reach us; and our movement in the digital world will be near telepathic.</div>
</td>
<td>
    <!-- this is a div with absolute position to hightlight the line only -->
    <div id="highlightingline" style="border-spacing: 5px; background-color: rgb(200, 226, 235); position: absolute; width: 55%; float: left; margin-left: -795px; opacity: 0.5; margin-top: 111px; color: black; border-collapse: separate; border-color: gray;">&nbsp;</div>
</td>
</tr>
</table>

请注意,我不想通过鼠标选择选择文本,因为它已经正常工作,我只想获得没有鼠标选择的高亮线文本。你可以说,我想获得文本上突出显示的行所在的行的文本。

请看看这个jsfiddle我创建了一个演示,看看这个 滚动您将看到的内容突出显示我想要获取文本的行,突出显示哪行/文本/句子

http://jsfiddle.net/qbhNw/1/

1 个答案:

答案 0 :(得分:2)

Working Fiddle

var text = "";
if (window.getSelection) {
    text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
    text = document.selection.createRange().text;
}

alert(text);