我想從一個div複製一個句子的特定部分到另一個,並使用JavaScript將其輸入到另一個區域。如何將特定文本從一個區域複製到另一個區域?
<div id="copyfromhere">This is +how+ it works.</div>
<div id="pastehere"></div>
我想複製+符號之間的部分。 +符號包含在原始語句中。
我想從一個div複製一個句子的特定部分到另一個,並使用JavaScript將其輸入到另一個區域。如何將特定文本從一個區域複製到另一個區域?
<div id="copyfromhere">This is +how+ it works.</div>
<div id="pastehere"></div>
我想複製+符號之間的部分。 +符號包含在原始語句中。
你可以從DIV文字和分裂它的基礎上「+」,這將返回數組,那麼你可以在1
var pastehereDiv = document.querySelector("#pastehere");
var copyfromDiv = document.querySelector("#copyfromhere")
pastehereDiv.textContent = copyfromDiv.textContent.split('+')[1];
謝謝!這很好。 – komelon12
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時才起作用。
謝謝!這很好。 – komelon12
告訴我們你嘗試過的東西,但沒有工作? – kukkuz
你總是想複製+符號之間的東西嗎?句子中可能還有其他+符號? – Daniel
您可以通過以下方式獲取幫助: https://stackoverflow.com/questions/8647216/get-content-of-a-div-using-javascript –