Automatically close all the other
tags after opening a specific
tag

前端 未结 6 514
日久生厌
日久生厌 2021-02-06 01:08

Here is my code.



        
6条回答
  •  执念已碎
    2021-02-06 01:23

    Whao, nobody answering about

    are using toogle event ?

    const All_Details = document.querySelectorAll('details');
    
    All_Details.forEach(deet=>{
      deet.addEventListener('toggle', toggleOpenOneOnly)
    })
    
    function toggleOpenOneOnly(e) {
      if (this.open) {
        All_Details.forEach(deet=>{
          if (deet!=this && deet.open) deet.open = false
        });
      }
    }
    1 Demo 1
    2 Demo 2
    3 Demo 3

提交回复
热议问题