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 not.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can do something like this:
<details id="element">
<p>Details</p>
</details>
<script>
var isOpen = ($("#element").attr("open") == "open");
alert ("Open = " + isOpen);
</script>
回答2:
You can use the toggle event:
var details = document.querySelector("details")
details.addEventListener("toggle", function() {
details.firstChild.textContent = "done"
})
<!doctype html>
<details>
<summary>toggle event</summary>
</details>
回答3:
In jQuery you can catch the event using .on('toggle')
like this:
$('#detail-id').on('toggle', function() {
//code
});