简体   繁体   中英

Remove div block and replace it by another

I have a js code that suppose to remove block1 and replace it with block2 by an onclick function

function buyerclick() {
  div = document.getElementById('block2');
  div.style.display = "block";
  var elem = document.getElementsByClassName('block1');
  elem.parentNode.remove(elem);
}

Block2 is working OK but block1 isn't remove it just stayed under block2. What I did wrong? I need that block1 removed with all its elements.

getElementsByClassName返回一个集合,因此您必须将该行更改为:

var elem = document.getElementsByClassName('block1')[0];

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