如何使用 javascript 将内容从文本区域移动到 div

How to move content from a textarea to a div using javascript

我想将内容从 <textarea> 移动到 <div>,但我真的不知道该怎么做。

这是我的代码:

<div id=one>
   <textarea id="mssg" rows="5" cols="40" >
       Hey, I am cooking. what are you doing?
   </textarea>
   <br>
</div>
<input type="button" value="swap" onclick="swap()">
<a href="solution.html" >Sign out</a>
<div id="two" >
    <div>Sent messages appear here:</div>
</div>
<script type="text/javascript">
    function swap()
    {
        var one = document.getElementById("one").innerHTML;
        two.innerHTML = "<div>"+one+"</div>";
    }
</script>

Here's an example.

<script type="text/javascript">
     function swap() {
         var one = document.getElementById("one");
         var one_inner = one.innerHtml;
         var two = document.getElementById("two");

         one.innerHtml = two.innerHtml;
         two.innerHtml = one_inner;

     }
</script>

这确实是一种奇怪的行为。无论如何,您交换的是内容,而不是元素。

希望对您有所帮助。

function swap()
{
    var one = document.getElementById("one").innerHTML;

    var two = document.getElementById("two");
    var twohtml = document.getElementById("two").innerHTML
    document.getElementById('one').remove();
    two.innerHTML =   "<div>"+twohtml+"</div>" + "<div>"+one+"</div>" ;

}