什么是shadow DOM?

An important aspect of web components is encapsulation — being able to keep the markup structure, style, and behavior hidden and separate from other code on the page so that different parts do not clash, and the code can be kept nice and clean. The Shadow DOM API is a key part of this, providing a way to attach a hidden separated DOM to an element. This article covers the basics of using the Shadow DOM.

下面这个是shadow DOM 的使用例子:

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>shadowDOM</title>
<style type="text/css">
#div {
width: 300px;
height: 50px;
border: 1px solid #666;
padding: 15px;
}
</style>
</head>
<body>
<div id="div">这里是不显示出来的</div>
<button>点我点我</button>
</body>
</html>

  javascript:

function createShadowDOM(elem) {
var root = elem.createShadowRoot();
root.appendChild(createStyle());
root.appendChild(createInputDiv("姓名","name"));
} function createStyle() {
var style = document.createElement('style');
style.textContent = 'div.input-div { height: 30px; width: 250px; }' +
'font.input-font { line-height: 30px;font-size: 16px;color: #495A80; margin-right: 10px;}'+
'span.input-area {width: 200px;height: 25px;line-height: 25px;padding-left: 5px;display:inline-block;color: #666;font-size: 16px;border: 1px solid #999;border-radius: 3px;}';
return style;
} function createInputDiv(font, name) {
var inputDiv = document.createElement('div');
inputDiv.className = 'input-div';
inputDiv.innerHTML = "<font class='input-font'>" + font + "</font><span class='input-area' contentEditable='true' id=" + name + "></span>";
return inputDiv;
} createShadowDOM(document.querySelector("#div")); document.querySelector('button').addEventListener('click', function() {
console.log(document.querySelector('#div').shadowRoot.querySelector('#name').innerHTML);
})

  结果:

This article assumes you are already familiar with the concept of the DOM (Document Object Model) — a tree-like structure of connected nodes that represents the different elements and strings of text appearing in a markup document (usually an HTML document in the case of web documents). As an example, consider the following HTML fragment:

两个其前端前沿网站:

js代码在线编辑:https://jsbin.com/?html,output

兼容性查询:https://caniuse.com/

----------------------------------------------------------------------------------------------------------

参考:https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM

https://blog.csdn.net/qq_31280709/article/details/75577439

html 中shadow DOM 的使用的更多相关文章

  1. 【Web技术】401- 在 React 中使用 Shadow DOM

    本文作者:houfeng 1. Shadow DOM 是什么 Shadow DOM 是什么?我们先来打开 Chrome 的 DevTool,并在 'Settings -> Preferences ...

  2. 【shadow dom入UI】web components思想如何应用于实际项目

    回顾 经过昨天的优化处理([前端优化之拆分CSS]前端三剑客的分分合合),我们在UI一块做了几个关键动作: ① CSS入UI ② CSS作为组件的一个节点而存在,并且会被“格式化”,即选择器带id前缀 ...

  3. 封印术:shadow dom

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<鼠标滚动插件smoovejs和wowjs> 作者主页:myvin 博主QQ:851399101(点击Q ...

  4. 使用shadow dom封装web组件

    什么是shadow dom? 首先我们先来看看它长什么样子.在HTML5中,我们只用写如下简单的两行代码,就可以通过 <video> 标签来创建一个浏览器自带的视频播放器控件. <v ...

  5. shadow dom

    初识shadow dom 我们先看个input="range"的表现: what amazing ! 一个dom能表现出这么多样式嘛? 无论是初学者和老鸟都是不肯相信的,于是在好奇 ...

  6. shadow dom 隔离代码 封装

    Shadow DOM是指浏览器的一种能力,它允许在文档(document)渲染时插入一棵DOM元素子树,但是这棵子树不在主DOM树中.   Shadow DOM 解决了 DOM 树的封装问题.     ...

  7. 纯CSS菜单样式,及其Shadow DOM,Json接口 实现

    先声明,要看懂这篇博客要求你具备少量基础CSS知识, 当然如果你只是要用的话就随便了,不用了解任何知识 完整项目github链接:https://github.com/git-Code-Shelf/M ...

  8. JavaScript 是如何工作:Shadow DOM 的内部结构 + 如何编写独立的组件!

    这是专门探索 JavaScript 及其所构建的组件的系列文章的第 17 篇. 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工作的:引擎,运行时和调用堆栈的概述! Jav ...

  9. Shadow DOM及自定义标签

    参考链接:点我 一.什么是Shadow DOM Shadow DOM,直接翻译的话就是 影子 DOM,可以理解为潜藏在 DOM 结构中并且我们无法直接控制操纵的 DOM 结构.类似于下面这种结构 Sh ...

随机推荐

  1. wxwidget wxpython 可视化开发工具

    wxwidget官方建议的工具集合:http://wiki.wxwidgets.org/Tools 支持wxpython可视化开发工具 wxFormBuilder wxGlade wxDesigner ...

  2. wikioi 1068 乌龟棋

    四维dp表示分别用了多少张卡后可以获得的最大分数 #include <cstdio> #include <cmath> #include <cstring> #in ...

  3. nginx新建nginx_fzjh.conf文件,不使用默认配置文件

    worker_processes 4; events{ worker_connections 1024; } http{ server { listen 80; server_name myserve ...

  4. ROWID面试题-删除表中重复数据(重复数据保留一个)

    /* ROWID是行ID,通过它一定可以定位到r任意一行的数据记录 ROWID DNAME DEPTNO LOC ------------------ ------------------------ ...

  5. 介绍一下开源项目FastAnimationWithPOP

    介绍一下开源项目FastAnimationWithPOP JUL 23RD, 2014 这是一个非常easy的动画框架,基于Facebook的POP库. 使用它你就能够在故事版中以0行代码的代价来加入 ...

  6. SYSCALL_DEFINE3

    http://blog.csdn.net/yueyingshaqiu01/article/details/48786961

  7. debian添加中文支持

    转载:http://www.shunix.com/debian-chinese-support-472/   debian与ubuntu有很大的相似性,但是debian相对更原始,比如在语言支持这一块 ...

  8. 关于linux系统端口查看和占用的解决方案

    原文:http://www.2cto.com/os/201411/355959.html 一直以来,在处理linux服务器的过程中,经常会遇到一个问题,有时候kill掉进程之后,端口被占用,新的进程一 ...

  9. extjs 动态设定 DateField 最大值 最小值

    yxrqDate.minValue = new Date();yxrqDate.maxValue = new Date(9000,1,1);yxrqDate.validate(); //var pic ...

  10. 10.2.2移动产品离线功能等具体解释----暨4月8日移动《在离线一体化》公开课Q&amp;A

    4月8日<离,或者不离,ArcGIS移动的"在离线一体化"就在那里!>移动公开课已经结束,针对公开课上粉丝们重点关注的问题,本博客进行了具体的解答.答疑主要环绕最新的R ...