代码之家  ›  专栏  ›  技术社区  ›  faressoft

如何从iframe中获取所选文本(处理IE)

  •  1
  • faressoft  · 技术社区  · 14 年前

    1 回复  |  直到 14 年前
        1
  •  3
  •   Tim Down    14 年前

    function getIframeSelectionText(iframe) {
        var win, doc = iframe.contentDocument;
        if (doc) {
            win = doc.defaultView;
        } else {
            win = iframe.contentWindow;
            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("your_iframe");
    alert( getIframeSelectionText(iframe) );