2017-12-22 10 views
0

ウェブページにiframeがあります。このiframeは、私のサーバーの.txtファイルに送信されています(つまり、その内容を示しています)。"iframe内のすべてのテキストを選択"ボタン?

クリックすると、iFrame内のすべてのテキストがに変更されます(iFrameの外にあるボタン)を実装したいと考えています。

これは可能ですか? (たとえば、jQuery/JavaScript経由)

+0

このWebページの下部にあるコードは機能しません。https://www.experts-exchange.com/questions/20699826/Selecting-all-text-within-an-iframe.html – Crickets

答えて

0

iframeには、frameまたはiframe要素によって生成されたドキュメントオブジェクトを返すcontentDocumentというDOMプロパティがあります。あなたはインラインフレームページのいずれかの方法を使用することができますiFrameName.window

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で動作させることができますが、実際のWebサイトで実装したときは驚くほど効果的でした。ありがとう! – Crickets

+0

うれしい – mac

+0

iOS上でこの「すべて選択」コードを動作させる方法を知っていますか? – Crickets

0

使用。

テキストを選択するには、https://code.google.com/p/rangy/を試してください。ただ単にすべてdocument.bodyiFrameName.window.document.bodyに置き換えてください。

単純な代替方法:iFrameName.window.document.execCommand("selectAll")を使用してください。

関連する問題