2017-08-24 132 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

相关问题