Detecting the opening or closing of a details element

前端 未结 7 1149
栀梦
栀梦 2021-01-01 13:07

How can I detect when a details element is opened or closed in Javascript? Other than attaching a listener to the click function and checking if the open attribute is set or

7条回答
  •  醉话见心
    2021-01-01 13:19

    You can use the toggle event:

    var details = document.querySelector("details")
    
    details.addEventListener("toggle", function() {
     details.firstChild.textContent = "done"
    })
    
    
    toggle event

提交回复
热议问题