【问题标题】:Get selected text in iframe & show it in a textbox(search)在 iframe 中获取选定的文本并将其显示在文本框中(搜索)
【发布时间】:2019-10-22 14:51:07
【问题描述】:

我想在 iframe 中获取一些选定的文本并在搜索框中显示该文本。

第一次使用javascript关于iframe,所以我真的不知道为什么这段代码不起作用。

你能修复这个代码吗?

jQuery 或 javascript

frameElement.onmouseup = function() {
    document.getElementById("console").innerHTML = selectText(iframe);
}


function selectText(iframe) {

var frame = document.getElementById(iframe);
var frameWindow = frame && frame.contentWindow; 
var frameDocument = frameWindow && frameWindow.document;
var selectionText = "SelectWord";    

if (frameDocument) { 
    if (frameDocument.getSelection) { 
        selectionText = String(frameDocument.getSelection()); 
    } 
    else if (frameDocument.selection) { 
        selectionText = frameDocument.selection.createRange().text; 
    } 
    else if (frameWindow.getSelection) { 
        selectionText = String(frameWindow.getSelection()); 
    } 
}

return selectionText;
}



</script>

上面一行是脚本部分

<body>

<iframe id="iframe" name="iframe" src="https://www.reuters.com/"></iframe>

<div id="console" class="div" type="search"></div>

</body>

【问题讨论】:

标签: javascript jquery html select iframe


【解决方案1】:

同源策略...

使用代理页面...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2018-08-26
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多