2017-12-22 2 views
0

웹 페이지에 iframe이 있습니다. 이 iframe은 내 서버에있는 .txt 파일에 소스로 있습니다 (즉, 내용을 보여줍니다)."iframe의 모든 텍스트 선택"버튼을 클릭하십시오.

클릭하면 버튼을 구현하고 싶습니다. 모두 텍스트를 iFrame에 선택합니다.

이것이 가능합니까? (예 : jQuery/JavaScript를 통해)

+0

이 웹 페이지의 맨 아래에있는 코드는 작동하지 않습니다. https://www.experts-exchange.com/questions/20699826/Selecting-all-text-within-an-iframe.html – Crickets

답변

0

프레임 또는 iframe 요소에 의해 생성 된 문서 객체를 반환하는 contentDocument라는 iframe에 대한 DOM 속성이 있습니다.

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); 
+0

수 없습니다. JSFiddle.net에서 작동하도록이 코드를 가져 오지만, 실제 웹 사이트에서 구현했을 때 놀라 울 정도로 잘 작동했습니다. 감사! – Crickets

+0

기꺼이 도와 드리겠습니다. – mac

+0

"Select All"코드가 iOS에서 작동하도록 만드는 방법을 알고 계십니까? – Crickets

0

사용 iFrameName.window 당신은 iframe이 페이지의 방법을 사용할 수 있습니다.

텍스트를 선택하려면 https://code.google.com/p/rangy/을 시도하십시오. 단지 document.bodyiFrameName.window.document.body으로 간단하게 대체하십시오.

또는 간단한 대체 방법 : 그냥 iFrameName.window.document.execCommand("selectAll")을 사용하십시오.

관련 문제