2021年2月1日星期一

Why does the connectedCallback not work for native HTMLElement(not custom elements)?

I know connectedCallback is for custom elements. I wonder how to make the following snippet work:

const div = document.createElement('div');  div.connectedCallback = () => console.log('connected!');  document.body.appendChild(div);  // how to make it print "connected!" here?  

There are guys making DOM/connectedCallback which utilizes the MutationObserver.

MutationObserver should work for this scenario, but why does W3C not make connectedCallback work for HTMLElement? for performance?

I found there is a read-only property isConnected but it's not helpful for detecting connected or not.

https://stackoverflow.com/questions/66003113/why-does-the-connectedcallback-not-work-for-native-htmlelementnot-custom-elemen February 02, 2021 at 11:06AM

没有评论:

发表评论