2009-11-13 16 views
-1

如何獲取iframe中的選定文本。iframe中的選定文本

我的頁面我有一個可編輯的iframe iframe。 那麼我怎樣才能得到該iframe中的選定文本。

+1

這是http://stackoverflow.com/questions的精確副本/ 1471759/how-to-get-selected-text-from-iframe-with-javascript – 2009-11-13 04:45:15

+0

@Dan McG - 然後投票! – 2009-11-13 04:51:47

+0

-1。我知道我忘了一些東西... – 2009-11-13 05:16:21

回答

0

林不知道究竟你被選中的文字是什麼意思,但是這是你如何訪問一個iframe的內容與jQuery

$('#iFrameID').contents().find('#whatImLookingFor').text(getSelectedText()); 

function getSelectedText(){ 
    if(window.getSelection){ 
     return window.getSelection().toString(); 
    } 
    else if(document.getSelection){ 
     return document.getSelection(); 
    } 
    else if(document.selection){ 
     return document.selection.createRange().text; 
    } 
} 
+0

選擇文本的意義(使用鼠標選定的文本) – Santhosh 2009-11-13 04:40:58

2

忽略了什麼我已經寫了,那是垃圾。

這是實打實的:

var iframe= document.getElementById('yourFrameId'); 
var idoc= iframe.contentDocument || iframe.contentWindow.document; // For IE. 
alert(idoc.getSelection()); 

以上是公然從bobince's答案被盜this SO Question

0

不知道你在iframe的意思編輯真實的東西。可能是文本框或者textarea。但是,如果你真的有一個iframe,在一些文本被選中,那麼這裏是一個代碼段,應該在一些瀏覽器的工作:

var iframe = document.getElementById("frame1"); 
var txt = ""; 
if (iframe.contentWindow.getSelection) { 
    txt = iframe.contentWindow.getSelection(); 
} else if (iframe.contentWindow.document.getSelection) { 
    txt = iframe.contentWindow.document.getSelection(); 
} else if (iframe.contentWindow.document.selection) { 
    txt = iframe.contentWindow.document.selection.createRange().text; 
}