"Select all text in iframe" 按钮?
"Select all text in iframe" button?
我在网页上有一个 iframe。此 iframe 源自(即显示其内容)我服务器上的一个 .txt
文件。
我想实现一个按钮(当然位于 iFrame 之外),单击该按钮时,全选 iFrame 中的文本。
这可能吗? (例如,通过 jQuery/JavaScript。)
有一个用于 iframe 的 DOM 属性 称为 contentDocument,它 returns 由框架或 iframe 元素生成的文档对象。
HTML
<iframe id="frame" src="file.txt"></iframe>
<!-- points to the text file -->
<button id="selectText">Copy Text</button>
<!-- button to copy the text -->
JS
var selButtom = document.getElementById('selectText'); //store the button value in a variable
var frame = document.getElementById('frame'); // store iframe in variable
selButtom.addEventListener("click", function(){
var frameContent = frame.contentDocument; // get content of iframe
frameContent.execCommand('selectAll'); // execute select command
},false);
使用iFrameName.window
可以使用iframe页面的任何方法
要select文,可以试试https://code.google.com/p/rangy/。只需简单地将所有 document.body
替换为 iFrameName.window.document.body
.
或者一个简单的替代方法:只需使用 iFrameName.window.document.execCommand("selectAll")
.
我在网页上有一个 iframe。此 iframe 源自(即显示其内容)我服务器上的一个 .txt
文件。
我想实现一个按钮(当然位于 iFrame 之外),单击该按钮时,全选 iFrame 中的文本。
这可能吗? (例如,通过 jQuery/JavaScript。)
有一个用于 iframe 的 DOM 属性 称为 contentDocument,它 returns 由框架或 iframe 元素生成的文档对象。
HTML
<iframe id="frame" src="file.txt"></iframe>
<!-- points to the text file -->
<button id="selectText">Copy Text</button>
<!-- button to copy the text -->
JS
var selButtom = document.getElementById('selectText'); //store the button value in a variable
var frame = document.getElementById('frame'); // store iframe in variable
selButtom.addEventListener("click", function(){
var frameContent = frame.contentDocument; // get content of iframe
frameContent.execCommand('selectAll'); // execute select command
},false);
使用iFrameName.window
可以使用iframe页面的任何方法
要select文,可以试试https://code.google.com/p/rangy/。只需简单地将所有 document.body
替换为 iFrameName.window.document.body
.
或者一个简单的替代方法:只需使用 iFrameName.window.document.execCommand("selectAll")
.