jquery - "Select all text in iframe"按钮?

标签 jquery html button iframe selectall

我的网页上有一个 iframe。该 iframe 源 self 服务器上的一个 .txt 文件(即显示其内容)。

我想实现一个按钮(当然位于 iFrame 之外),单击该按钮时,选择 iFrame 中的所有文本。

这可能吗? (例如,通过 jQuery/JavaScript。)

最佳答案

iframe 有一个名为 contentDocument 的 DOM 属性,它返回由框架或 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);

关于jquery - "Select all text in iframe"按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47937586/

相关文章:

javascript - 使用 ng-pattern Angularjs 进行动态验证

javascript - 如何在任何网站上更改标签值

html - slider 将按钮转换为箭头

jquery - window.opener 未将值返回到右侧文本框

javascript - 获取 mouseDown 上的类或 id 值

javascript - 使用 Javascript/JQuery 动态创建具有不同点击事件处理程序的多个 HTML 按钮

php - Zend Paginator - 页面链接

html - 导航栏链接的背景颜色

php - 使用按钮控制 mysql 数组

python tkinter 按钮事件