我想指定firefox选择一个范围。我可以使用 IE 轻松做到这一点,使用 range.select();。FFX 似乎需要一个 dom 元素。我错了,还是有更好的方法来解决这个问题?
我首先获取文本选择,将其转换为范围(我认为?)并保存文本选择。这是我最初得到范围的地方:
// Before modifying selection, save it
var userSelection,selectedText = '';
if(window.getSelection){
userSelection=window.getSelection();
}
else if(document.selection){
userSelection=document.selection.createRange();
}
selectedText=userSelection;
if(userSelection.text){
selectedText=userSelection.text;
}
if(/msie|MSIE/.test(navigator.userAgent) == false){
selectedText=selectedText.toString();
}
origRange = userSelection;
我后来更改了选择(成功)。我通过 IE 中的范围和 ffx 中的 dom ID 来执行此操作。但是在我这样做之后,我想将选择设置回原始选择。
这就像 IE 中的魅力:
setTimeout(function(){
origRange.select();
},1000);
我想在 FFX 中做这样的事情:
var s = w.getSelection();
setTimeout(function(){
s.removeAllRanges();
s.addRange(origRange);
},1000);
不幸的是,FFX 没有合作,这不起作用。有任何想法吗?