IE选择和范围问题 [英] Issue with IE Selection and Range

查看:91
本文介绍了IE选择和范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取选择的开始元素和结束元素以及每个选择的偏移量,我在Firefox中按如下方式进行操作:

I'm trying to get the start element and the end element of a selection and the offset of the selection in each, i do this in firefox as follows:

var delselection = window.getSelection();

var startOffset = delselection.anchorOffset;

var endOffset = delselection.focusOffset;

var startNode = delselection.anchorNode.parentNode;

var endNode = delselection.focusNode.parentNode;

但是我不知道如何在IE6中做到这一点,有谁能向我指出正确的方向?

However i have no idea how to do this in IE6, anyone able to point me in the right direction?

推荐答案

document.selection.

document.selection.

但是IE返回的TextRange对象与Firefox/WebKit/W3不匹配,确定起点和终点的确切位置非常令人沮丧.根据您对范围所做的确切操作,您可以使用range.parentElement(),range.inRange()或range.compareEndPoints()到达某个地方.对于富文本编辑器,您通常最终会使用极其丑陋的range.execCommand()接口.

However the TextRange object returned by IE does not match Firefox/WebKit/W3's, and determining the exact positions of the start and end points is very frustrating. Depending on what exactly you are doing with the range you may be able to get somewhere with range.parentElement(), range.inRange() or range.compareEndPoints(). For rich text editors you will usually end up using the staggeringly ugly range.execCommand() interface.

IE Range的实现是如此奇怪,与Mozilla/Webkit/W3模型不同,以至于通常情况下,与两者之间的选择有关的所有事情都以完全不同的代码路径结束.

The IE Range implementation is so odd and different to the Mozilla/Webkit/W3 model that you typically end up with completely different code paths for everything to do with selections between the two.

这篇关于IE选择和范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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