如何在具有“开始”的iframe中选择范围?和“结束”在Firefox中,例如“ selectionStart”来自输入元素 [英] How To Select A Range in an Iframe With "Start" and "End" in Firefox like "selectionStart" from inputs element

查看:81
本文介绍了如何在具有“开始”的iframe中选择范围?和“结束”在Firefox中,例如“ selectionStart”来自输入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Internet Explorer,我具有当前代码,可以在desingMode设置为on的iframe中选择一个范围:

for Internet Explorer, I have the current code to select a range in an iframe with desingMode setting to on:


    var Range = window.document.selection.createRange();
    var obj = { start: 3, end : 6}
    Range.collapse( true );
    Range.moveStart( 'character', obj.start );
    Range.moveEnd( 'character', obj.end - obj.start );
    Range.select();

如果我想仅通过2个参数选择一个字符串,则最有用。开始和结束(对于输入元素,存在属性selectionStart和selectionEnd。)。

Most useful if I want select only one piece of a string by only 2 parameters. Start and End ( for input elements exists the properties selectionStart and selectionEnd ).

例如,在字符串 Hello World上执行此代码时,它仅突出显示字符串 llo Wo 之类的东西。

问题是Firefox Dom不支持moveStart或moveEnd方法,而仅支持 range.setStart 和range.setEnd,它们仅请求一个节点和一个偏移量作为参数。

When this code is executed, for example, on a string "Hello World", it highlight only the piece of string llo Wo, or something like that.
The problem is that Firefox Dom do not support the method moveStart or moveEnd, but only range.setStart and range.setEnd which request only a node and an offset as arguments.

那么可以在Firefox中虚拟化moveStart和moveEnd方法吗?
谢谢。

So is possible to virtualizing the moveStart and the moveEnd methods in Firefox? Thanks.

推荐答案

这对我有用:

var iframeElement = ...; // the DOM element for the iframe;

var contentDoc = iframeElement.contentDocument;
var range = contentDoc.createRange();
range.setStart(contentDoc.body.firstChild, 3);
range.setEnd(contentDoc.body.firstChild, 6);
var selection = iframeElement.contentWindow.getSelection();
selection.removeAllRanges();
selection.addRange(range);

您需要在Firefox中调用removeAllRanges(),否则最终可能会有多个选择。

You need to call removeAllRanges() in Firefox or else you can end up with multiple selections.

这篇关于如何在具有“开始”的iframe中选择范围?和“结束”在Firefox中,例如“ selectionStart”来自输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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