简体   繁体   中英

How to set a div's content to another div's content

So I have two divs

<div id="1"><p>Hello<p></div>
<div id="2"><p>Goodbye<p></div>

And I am wondering if there is a simple technique to set div "1" equal to div "2" ?

So, the end result will be:

<div id="1"><p>Goodbye<p></div>
<div id="2"><p>Goodbye<p></div>

Thanks.

Do exactly that..

var div1 = document.getElementById("1");
document.getElementById("2").innerHTML = div1.innerHTML;

Try it using javascript

document.getElementById("1").innerHTML = document.getElementById("2").innerHTML

to get specific tags within an element use:

var x = document.getElementById("1");
var y = x.getElementsByTagName("p"); 

or in one line:

document.getElementById("1").getElementsByTagName("p").innerHTML;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM