如何将getSelection与iframe一起使用

时间:2014-07-30 04:02:59

标签: javascript iframe getselection

我一直在尝试使用getSelection命令从iframe中选择突出显示的文本并将其插入到变量中。使用以下代码,我可以使这项工作成为一个textarea。

   <script type="text/javascript">
    function GetSelectedText () {
        var selText = "";
        if (window.getSelection) {
            if (document.activeElement && 
                    (document.activeElement.tagName.toLowerCase () == "textarea" ))
            {
                var text = document.activeElement.value;
                selText = text.substring (document.activeElement.selectionStart, 
                                          document.activeElement.selectionEnd);
            }
            else {
                var selRange = window.getSelection ();
                selText = selRange.toString ();
            }
        }


        else {
            if (document.selection.createRange) {
                var range = document.selection.createRange ();
                selText = range.text;
            }
        }

var highlight = selText;
        if (highlight !== "") {
            alert (highlight);
        } 
        if (highlight == "") {
            alert ("No string selected");
        }


    } </script>
<body >
<br /><br />
<textarea  onclick="GetSelectedText ()">Some text in a textarea element.</textarea>
</body>

但是当我添加以下代码行时,它仍然不会在iframe中执行。

|| document.activeElement.tagName.toLowerCase () == "iframe"

如何解决此问题,以便我可以突出显示iframe中的字符串并让脚本执行?

更新:

    <script type="text/javascript">
    var editorDoc;
    function InitEditable () {
        var editor = document.getElementById ("editor");

        if (editor.contentDocument)
            editorDoc = editor.contentDocument;
        else
            editorDoc = editor.contentWindow.document;

        var editorBody = editorDoc.body;

        if ('contentEditable' in editorBody) {
                // allow contentEditable
            editorBody.contentEditable = true;
        }
        else {  // Firefox earlier than version 3
            if ('designMode' in editorDoc) {
                    // turn on designMode
                editorDoc.designMode = "on";                
            }
        }
    }
    var iframe = document.getElementById('editor');
var innerDoc = iframe.contentDocument;

function GetSelectedText () {
    var selText = "";
    if (innerDoc.getSelection) {
        if (innerDoc.activeElement && 
                (innerDoc.activeElement.tagName.toLowerCase () == "iframe"))
        {
            var text = innerDoc.activeElement.value;
            selText = text.substring (innerDoc.activeElement.selectionStart, 
                                      innerDoc.activeElement.selectionEnd);
        }
        else {
            var selRange = innerDoc.getSelection ();
            selText = selRange.toString ();
        }
    }


    else if
         (innerDoc.selection.createRange) {
            var range = innerDoc.selection.createRange ();
            selText = range.text;
        }


var highlight = selText;
    if (highlight !== "") {
        alert (highlight);
    } 
    if (highlight == "") {
        alert ("No string selected");
    }


}

</script>

<body onload="InitEditable ();">

<iframe onclick="GetSelectedText ()" id="editor"></iframe>

</body>

1 个答案:

答案 0 :(得分:1)

听起来这个脚本在iframe的父窗口中运行。因此,您需要使用与document不同的对象来访问它:

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument;