2017-08-24 118 views
0

我想從一個div複製一個句子的特定部分到另一個,並使用JavaScript將其輸入到另一個區域。如何將特定文本從一個區域複製到另一個區域?

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

我想複製+符號之間的部分。 +符號包含在原始語句中。

+1

告訴我們你嘗試過的東西,但沒有工作? – kukkuz

+0

你總是想複製+符號之間的東西嗎?句子中可能還有其他+符號? – Daniel

+0

您可以通過以下方式獲取幫助: https://stackoverflow.com/questions/8647216/get-content-of-a-div-using-javascript –

回答

0

你可以從DIV文字和分裂它的基礎上「+」,這將返回數組,那麼你可以在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>
訪問索引,數組第二個值

顯然這隻會在您的原始div中沒有​​任何其他+ s時才起作用。

+0

謝謝!這很好。 – komelon12

相關問題