[英]document.getSelection().toString() is always “ ” in ie while working with iframe
I wanted to get the selected text onmouseup. 我想获取选定的文本onmouseup。
I also tried 我也试过
setTimeout(() => {
var selectedData = contentWrapper.getSelection().toString();
alert(selectedData)
}, 1008)
html code HTML代码
<iframe src="url"></iframe>
js code js代码
var contentWrapper=document.getElementsByTagName("iframe")[0].contentDocument;
contentWrapper.body.addEventListener('mouseup', this.CheckSelections, false);
function CheckSelections() {
var selectedData = contentWrapper.getSelection().toString();
alert(selectedData)
}
This way it works in IE, Chrome & FF (testing on local), but I am not able to touch iframe here. 这样,它就可以在IE,Chrome和FF(在本地测试)中工作,但是我无法在此处触摸iframe。
function showSelection() { var selectedData = document.getSelection().toString(); alert(selectedData) } var contentWrapper; function attachIF() { if(location.href == "https://stacksnippets.net/js") return; var ifr = document.createElement("iframe"); ifr.src="javascript:'"+ // Access is denied. Here on stackoverflow.com "<script>window.onload=function(){"+ "document.write(\\\\'<script>if(document.domain)document.domain=\\\\\\""+document.domain+"\\\\\\";"+ "document.write(\\""+parent.document.body.innerText+"\\");"+ "<\\\\\\\\/script>\\\\');"+ "document.close();"+ "parent.contentWrapper = document;"+ "document.body.onmouseup=parent.CheckSelections;"+ "};<\\/script>'"; document.body.appendChild(ifr); } function CheckSelections() { var selectedData = contentWrapper.getSelection().toString(); alert(selectedData) }
<body onmouseup="showSelection()" onload=attachIF()> <iframe src="https://stacksnippets.net/js"></iframe> Some text to be selected. </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.