Javascript如何在IFrame中获取选择位置?

时间:2017-08-24 10:53:00

标签: javascript jquery html css iframe

我有一个iframe,用户可以输入他们想要的任何内容。让我们说他们有一个很长的段落,并突出显示其中的一个单词。然后他们单击一个按钮,这将运行一个javascript函数。在我的功能中,我能够获得突出显示的单词并存储它但我无法获得选择的开始和结束位置。到目前为止,这是我的代码:

<center><iframe name="richTextField" id="richTextField" ><?php echo $text;?></iframe></center>
<a class="buttonOnBar" id="btn14_quote" type="button" value="" onClick="javascript:iquote();" title="Quoteify"><img id="btnOnBarImg"src="img/icons/quote.png"></a>
function iquote(){

var len = document.getElementById("richTextField").contentWindow.getSelection().toString().length;

var start = document.getElementById("richTextField").contentWindow.document.selectionStart;
alert(start);
var end= document.getElementById("richTextField").contentWindow.document.selectionEnd;
var selectedText = document.getElementById("richTextField").contentDocument.getSelection();

}

selectionEnd或selectionStart都没有给我文本iframe中的位置。它只是说未定义。我做错了什么?

0 个答案:

没有答案