Detecting the opening or closing of a details element

前端 未结 7 1014
栀梦
栀梦 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:37

    To test the current state, without events listeners, we can simply check if the attribute open is set:

    // Test
    onclick = () => {
      console.log(
        !detailElem.hasAttribute("open")
      )
    }
    <details id="detailElem">
      <summary>Some details</summary>
    </details>

    0 讨论(0)
提交回复
热议问题