我想从一个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 –