简体   繁体   English

HTML中的Shadow dom

[英]Shadow dom in HTML

I've been hearing about Shadow DOM recently. 我最近听说过Shadow DOM。 While watching a video about the release of Angular 2 the presenter repeatedly made mention of Shadow DOM without clear expressions. 在观看有关Angular 2发行的视频时,演示者反复提到Shadow DOM,但没有明确的表达。 What does Shadow DOM really mean? Shadow DOM的真正含义是什么?

The best explanation I've found is this from What the Heck is Shadow DOM? 我发现的最好的解释是《 什么是影子DOM? shown below: 如下图所示:

Shadow DOM refers to the ability of the browser to include a subtree of DOM elements into the rendering of a document, but not into the main document DOM tree. 影子DOM是指浏览器将DOM元素的子树包含到文档的呈现中,而不是包含在主文档DOM树中的能力。

An important use case would be with video controls on a web page. 一个重要的用例是网页上的视频控件。 The markup only shows a video tag, with some attributes and source tags. 标记仅显示带有某些属性和源标签的视频标签。 The addtional code for all the video operations to work is hidden in the Shadow DOM and not available to the rest of the page. 使所有视频操作正常工作的附加代码隐藏在Shadow DOM中,该页面的其余部分不可用。 The actual markup, javascript, and styles for the tag are encapsulated, which hides the implementation details of the video controls that each browser vendor has already written. 标记的实际标记,javascript和样式被封装,从而隐藏了每个浏览器供应商已经编写的视频控件的实现细节。

So while it's there in the DOM, its hidden from the page that renders it. 因此,尽管它在DOM中存在,但在呈现它的页面中隐藏了。 So, to view the Shadow DOM, you can enable it under Dev Tools in Chrome. 因此,要查看Shadow DOM,可以在Chrome中的“开发工具”下启用它。

The short answer is that the Shadow DOM is one of four technologies that make up Web Components. 简短的答案是,影子DOM是构成Web组件的四种技术之一。

For a definition, Web Components are: A component platform from the W3C that allows Web sites to be constructed from standardized building blocks. 对于定义,Web组件是:W3C的组件平台,允许从标准化的构建基块构建网站。 Web Components comprise Custom Elements, Shadow DOM and HTML Imports and Templates. Web组件包括自定义元素,Shadow DOM和HTML导入和模板。

Shadow DOM is a technology of Web Components (although each can be used separately): Shadow DOM是一种Web组件技术(尽管每个组件可以单独使用):

  1. Custom Elements: is a capability for creating your own custom HTML tags and elements. 自定义元素:一种用于创建自己的自定义HTML标签和元素的功能。 They can have their own scripted behavior and CSS styling. 他们可以具有自己的脚本行为和CSS样式。 They are part of Web Components but they can also be used by themselves. 它们是Web组件的一部分,但也可以单独使用。
  2. HTML Templates: The HTML template element is a mechanism for holding client-side content that is not to be rendered when a page is loaded but may subsequently be instantiated during runtime using JavaScript. HTML模板:HTML模板元素是一种用于保存客户端内容的机制,该内容在页面加载时不呈现,但随后可以在运行时使用JavaScript实例化。 Think of a template as a content fragment that is being stored for subsequent use in the document. 可以将模板视为存储在文档中以供后续使用的内容片段。
  3. Shadow DOM: provides encapsulation for the JavaScript, CSS, and templating in a Web Component. 影子DOM:为Web组件中的JavaScript,CSS和模板提供封装。 Shadow DOM makes it so these things remain separate from the DOM of the main document. Shadow DOM可以使这些东西与主文档的DOM保持分离。 You can also use Shadow DOM by itself, outside of a web component. 您也可以在Web组件外部单独使用Shadow DOM。
  4. HTML Imports: is intended to be the packaging mechanism for Web Components, but you can also use HTML Imports by itself. HTML导入:旨在用作Web组件的打包机制,但是您也可以单独使用HTML导入。 You import an HTML file by using a tag in an HTML document. 您通过使用HTML文档中的标记导入HTML文件。

See Introduction to the Shadow DOM . 请参阅Shadow DOM简介

It refers to the ability to create a "child" DOM completely sandboxed from the rest of the page. 它指的是完全从页面其余部分沙盒创建“子” DOM的能力。 Useful for web components, reusable "widgets" which allow to not worry about their css/js affecting things they shouldn't. 对于Web组件很有用,可重用的“小部件”使您不必担心它们的css / js会影响它们不应该使用的东西。 http://glazkov.com/2011/01/14/what-the-heck-is-shadow-dom/ http://glazkov.com/2011/01/14/what-the-heck-is-shadow-dom/

Check out https://www.polymer-project.org/ if you want to see it in action. 如果您想查看它的实际运行,请查看https://www.polymer-project.org/

Think of shadow DOM is as an encapsulated (private) DOM. 将影子DOM视为封装的(私有)DOM。 You can't access the shadow DOM in the manner you access regular DOM, like 'document.querySelector()'. 您无法以访问常规DOM的方式(例如“ document.querySelector()”)访问影子DOM。

Let's say, you defined a reusable custom element, (which contains its DOM trees). 假设您定义了一个可重用的自定义元素(包含其DOM树)。 Then you use the custom element within your app HTML. 然后,您可以在应用HTML中使用自定义元素。

Now, the DOM under (which is now called a "host element") has become a shadow DOM subtrees (under a shadow root), hidden from the parent structure! 现在,其下的DOM(现在称为“主机元素”)已成为一个隐藏在父结构中的影子DOM子树(在影子根下)!

I hope this help you a bit. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM