范围选择和 Mozilla
我想指定 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 一直不合作,这不起作用。有什么想法吗?
I would like to specify that firefox select a range. I can do this easily with IE, using range.select();. It appears that FFX expects a dom element instead. Am I mistaken, or is there a better way to go about this?
I start by getting the text selection, converting it to a range (I think?) and saving the text selection. This is where I'm getting the range from initially:
// 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;
I later change the selection (successfully). I do so by range in IE and by a dom ID in ffx. But after I do that, I want to set back the selection to the original selection.
This works like a charm in IE:
setTimeout(function(){
origRange.select();
},1000);
I would like to do something like this in FFX:
var s = w.getSelection();
setTimeout(function(){
s.removeAllRanges();
s.addRange(origRange);
},1000);
Unfortunately, FFX has not been cooperative and this doesn't work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答是:IE 和其他浏览器在使用 JavaScript 选择文本的实现上有所不同(IE 有其专有的方法)。查看使用 JavaScript 选择文本。
另请参阅 MDC 上的 setSelectionRange。
编辑:做了一些测试用例后,问题就变得清晰了。
在 Firefox、Chromium 和 Opera 中测试此功能时,调试工具显示在
reselectFunc
中调用removeAllRanges
后,savedRange
和origRange< /code> 已重置。使用这样的对象调用
addRange
会导致 Firefox 中抛出异常:无需说,在所有三个浏览器中都没有选择任何文本。
显然这是预期的行为。分配了 (DOM)Selection 对象的所有变量在调用
removeAllRanges
。The short answer is: IE and other browsers differ in their implementations of selecting text using JavaScript (IE has its proprietary methods). Have a look at Selecting text with JavaScript.
Also, see setSelectionRange at MDC.
EDIT: After making a little test case, the problem becomes clear.
When testing this in Firefox, Chromium and Opera, the debugging tools show that after invoking
removeAllRanges
inreselectFunc
, bothsavedRange
andorigRange
are reset. InvokingaddRange
with such an object causes an exception to be thrown in Firefox:No need to say that in all three browsers no text is selected.
Apparently this in intended behaviour. All variables assigned a (DOM)Selection object are reset after calling
removeAllRanges
.谢谢马塞尔。你是对的,技巧是克隆范围,然后删除特定的原始范围。这样我们就可以恢复到克隆的范围。您的帮助引导我找到了下面的代码,它将选择切换到其他地方,然后根据超时返回。
没有你我不可能做到这一点,并给予你正确的答案:D
Thank you Marcel. You're right, the trick is to clone the range, then remove the specific original range. This way we can revert to the cloned range. Your help led me to the below code, which switches the selection to elsewhere, and then back according to a timeout.
I couldn't have done it without you, and grant you the correct answer for it :D