本文介绍了attachShadow 与 createShadowRoot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 mozilla 文档,Element.createShadowRoot() 已弃用:

I read in mozilla doc, Element.createShadowRoot() is deprecated:

此方法已被弃用,取而代之的是 attachShadow.

但在我的金丝雀中:chrome 49.0.2599.0

But in my canary: chrome 49.0.2599.0

就是这样:

 var shadow = document.getElementById("node-sh").createShadowRoot();  

那是行不通的

 var shadow = document.getElementById("node-sh").attachShadow({mode: 'closed'});

有人知道什么是对的吗?

Anyone know what's right?

推荐答案

createShadowRoot() 是将 shadow-root 附加到宿主元素的旧方法.它是在最初的规范中提出的,后来被弃用了附加阴影.规范也已更新.

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.

但是,新的 API 尚未标准化,并且没有浏览器目前支持它.所以我建议现在坚持使用 createShadowRoot.一旦你开始收到浏览器的弃用警告,那就是时候转向 attachShadow.

But, new API hasn't been standardized and none of the browsers currently support it. So I would suggest to stick to createShadowRootfor now. Once you start getting browser warnings for deprecation, that would be the time to move to attachShadow.

这篇关于attachShadow 与 createShadowRoot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 17:49