2017-08-24 3 views
0

divから別の部分に文の特定の部分をコピーし、JavaScriptを使用して別の領域に入力しようとしています。特定のテキストをあるエリアから別のエリアにコピーするにはどうすればよいですか?

<div id="copyfromhere">This is +how+ it works.</div> 
<div id="pastehere"></div> 

+記号の間に部分をコピーしたいと思います。 +記号は原文に含まれています。あなたは配列を返しますされ、DIVからテキストを取得し、「+」に基づいて、それを分割することができます

+1

あなたが試したことがあり、うまくいかなかったものを教えてください。 – kukkuz

+0

+記号の間に何かをコピーしたいですか?文に他の+記号があるかもしれませんか? – Daniel

+0

あなたは助けを得ることができます: https://stackoverflow.com/questions/8647216/get-content-of-a-div-using-javascript –

答えて

0

、あなたは1

var pastehereDiv = document.querySelector("#pastehere"); 
var copyfromDiv = document.querySelector("#copyfromhere") 
pastehereDiv.textContent = copyfromDiv.textContent.split('+')[1]; 
+0

ありがとう!これは素晴らしいです。 – komelon12

0

var copy = function() { 
 
    // grab the origin and destination divs 
 
    var origin = document.getElementById("copyfromhere"); 
 
    var destination = document.getElementById("pastehere"); 
 
    
 
    // get the text from the origin, split on "+" and then get the second element in the array 
 
    var text = from.innerText.split("+")[1]; 
 
    // apply that text to the destination div 
 
    destination.innerText = text; 
 
}
<div id="copyfromhere">This is +how+ it works.</div> 
 
<div id="pastehere"></div> 
 

 
<button id="copy" onClick="copy()">Copy</button>
でインデックス化その配列2番目の値にアクセスすることができます

明らかに、これは元のdivに他の+がない場合にのみ機能します。

+0

ありがとう!これは素晴らしいです。 – komelon12

関連する問題