如何将特定文本从一个区域复制到另一个区域?

时间:2017-08-24 17:14:07

标签: javascript

我正在尝试将句子的特定部分从div复制到另一个,并使用JavaScript将其输入到另一个区域。

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

我想复制+符号之间的部分。 +符号包含在原始句子中。

2 个答案:

答案 0 :(得分:0)

您可以从div获取文本并根据&#39; +&#39;将其分开,这将返回数组,然后您可以访问索引为1的数组第二个值。

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

答案 1 :(得分:0)

&#13;
&#13;
for ( var i = 0; i < arr.length; i++ ) {
    squares[ arr[ i ] ].css( 'opacity', '0.5' );
    setTimeout( function (x) {
        return function () {
            squares[ x ].css( 'opacity', '1' )
        }
    }( arr[ i ] ), 500 );
}
&#13;
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;
}
&#13;
&#13;
&#13;

显然,只有在您的原始div中没有​​任何其他<div id="copyfromhere">This is +how+ it works.</div> <div id="pastehere"></div> <button id="copy" onClick="copy()">Copy</button>时,这才有效。