简体   繁体   English

如何从iframe获取所选文本(在IE上运行)

[英]How to get selected text from iframe (working on IE)

如何从iframe获取所选文本(在IE上运行)

The following will work in all major browsers: 以下内容将在所有主流浏览器中运行:

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) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM