2009-09-24 21 views
17
<html> 
<body> 
    <script type="text/javascript"> 

    function smth() { 

    if (document.getSelection) { 
    var str = document.getSelection(); 
    if (window.RegExp) { 
     var regstr = unescape("%20%20%20%20%20"); 
     var regexp = new RegExp(regstr, "g"); 
     str = str.replace(regexp, ""); 
    } 
    } else if (document.selection && document.selection.createRange) { 
    var range = document.selection.createRange(); 
    var str = range.text; 
    } 

    alert(str); 
    } 
    </script> 

    <iframe id="my" width="300" height="225"> 
    .....some html .... 
    </iframe>  

    <a href="#" onclick="smth();">AA</a> 
</body>  
</html> 

smth函數我可以從某些div中獲取選定的文本,但它不適用於iframe。 任何想法如何從iframe中獲取選定的文本?如何從javascript中的iframe中選擇文本?

回答

6

您無法訪問iframe中來自不同於您的域的數據。 這是由於Same origin policy

+2

OK,對不起錯誤的榜樣,在IFRAME我有文字,我使用iframe的所見即所得的編輯器,所以它不是在不同的領域。 – 2009-09-24 13:47:16

+0

感謝您的信息。 – 2017-02-09 11:58:35

6

您需要從iframe中的文檔/窗口中選擇。

function getIframeSelectionText(iframe) { 
    var win = iframe.contentWindow; 
    var doc = win.document; 

    if (win.getSelection) { 
    return win.getSelection().toString(); 
    } else if (doc.selection && doc.selection.createRange) { 
    return doc.selection.createRange().text; 
    } 
} 

var iframe = document.getElementById("my"); 
alert(getIframeSelectionText(iframe)); 
17

document.getSelection

是在外部文檔。要獲得文件的選擇在iframe中,你需要獲取內部文件:

var iframe= document.getElementById('my'); 
var idoc= iframe.contentDocument || iframe.contentWindow.document; // ie compatibility 

idoc.getSelection() 

然而要注意的WebKit不支持document.getSelection()document.selection。試着用window.getSelection()這在Firefox和WebKit的作品,但返回一個選擇對象(集合/包裝周圍範圍),這就需要穿線替換它:

var idoc= iframe.contentDocument || iframe.contentWindow.document; 
var iwin= iframe.contentWindow || iframe.contentDocument.defaultView; 

''+iwin.getSelection() 

我不知道這點是什麼:

if (window.RegExp) { 
    var regstr = unescape("%20%20%20%20%20"); 
    var regexp = new RegExp(regstr, "g"); 
    str = str.replace(regexp, ""); 
} 

RegExp是基本JavaScript的追溯到最早的版本;它會永遠在那裏,你不必嗅探它。多個空格的URL編碼是非常不必要的。您甚至不需要RegExp,因此可以將字符串替換寫爲:

str= str.split('  ').join(''); 
+0

window.getSelection()返回一個選擇對象,而不是Range。 toString在選擇上會給你選擇文字。 – 2009-09-24 14:39:58

+0

是的,'''+'是toString的一個成語。 – bobince 2009-09-24 15:15:09

+0

是的,我知道。我在更正window.getSelection()返回的選擇對象的範圍時糾正了一個小錯誤,但同意你的代碼可行。 – 2009-09-24 15:31:35

2

以下代碼將返回選定的文本。

function getSelectedText(frameId) { 
    // In ExtJS use: 
    // var frame = Ext.getDom(frameId); 
    var frame = document.getElementById(frameId); 

    var frameWindow = frame && frame.contentWindow; 
    var frameDocument = frameWindow && frameWindow.document; 

    if (frameDocument) { 
     if (frameDocument.getSelection) { 
      // Most browsers 
      return String(frameDocument.getSelection()); 
     } 
     else if (frameDocument.selection) { 
      // Internet Explorer 8 and below 
      return frameDocument.selection.createRange().text; 
     } 
     else if (frameWindow.getSelection) { 
      // Safari 3 
      return String(frameWindow.getSelection()); 
     } 
    } 

    /* Fall-through. This could happen if this function is called 
     on a frame that doesn't exist or that isn't ready yet. */ 
    return ''; 
} 

希望這對別人有幫助。

+0

在控制檯中的firefox:錯誤:權限被拒絕訪問屬性'文檔'「。它在行是:」var frameDocument = [... ]」。 – Piotrek 2013-06-30 17:18:04

1

這個代碼在所有現代瀏覽器:

var iframe = document.getElementById('my'); 
var idoc = iframe.contentDocument || iframe.contentWindow.document; // ie compatibility 
var text = idoc.getSelection().toString(); 
相關問題