问题
I read in mozilla doc, Element.createShadowRoot() is deprecated:
This method has been deprecated in favor of attachShadow.
But in my canary: chrome 49.0.2599.0
thats work:
var shadow = document.getElementById("node-sh").createShadowRoot();
And thats not work
var shadow = document.getElementById("node-sh").attachShadow({mode: 'closed'});
Anyone know what's right?
回答1:
createShadowRoot()
is the old way of attaching a shadow-root to host element. It was proposed in initial spec, which has then been deprecated in favor of
attachShadow
.
Spec also has been updated.
But, new API hasn't been standardized and none of the browsers currently support it. So I would suggest to stick to createShadowRoot
for now. Once you start getting browser warnings for deprecation, that would be the time to move to attachShadow
.
来源:https://stackoverflow.com/questions/34452028/attachshadow-vs-createshadowroot