如何使用Javascript获取跨度选定部分 [英] how to get span selected portion using Javascript

查看:84
本文介绍了如何使用Javascript获取跨度选定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在一个范围内获得突出显示的selectionstart和selectedlenght?



谢谢

解决方案

我在 IERange 中使用我自己的算法优化,它为IE的TextRange提供了一个包装器(这是你从IE中的选择得到的),为它们提供与DOM范围相同的接口。



要获得选择文档,使用如下内容:

  var sel = window.getSelection(); //由IERange在IE中提供,内置于其他浏览器中
var range = sel.getRangeAt(0); //注意这在Safari 2中不起作用

range 现在有属性 startContainer startOffset ,它们分别是对节点的引用和其中的偏移量表示选择开始的节点,以及表示选择结束的相应属性 endContainer endOffset 。 / p>

is there a way to get the highlighted selectionstart and selectedlenght on a span ?

Thanks

解决方案

I use my own optimization of the algorithms in IERange, which provides a wrapper around IE's TextRange (which is what you get from the selection in IE) to give them the same interface as DOM Ranges.

To get the selection in the document, use something like the following:

var sel = window.getSelection(); // Provided by IERange in IE, built-in in other browsers
var range = sel.getRangeAt(0); // Note this doesn't work in Safari 2

range now has properties startContainer and startOffset, which are respectively a reference to a node and an offset within that node that represent the start of the selection, and corresponding properties endContainer and endOffset that represent the end of the selection.

这篇关于如何使用Javascript获取跨度选定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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