Created
July 7, 2017 03:43
-
-
Save dongyuwei/57b9edc674e53b3a89d75aeb247d8431 to your computer and use it in GitHub Desktop.
Shadow DOM demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Shadow DOM</title> | |
<style> | |
span { | |
color: green; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="hostElement"></div> | |
<script> | |
var hostElement = document.querySelector('#hostElement'); | |
var shadow; | |
if (hostElement.attachShadow) { | |
//Chrome Safari Opera, in chrome dev-tools check "Show user agent shadow DOM" | |
shadow = hostElement.attachShadow({mode: 'open'}); | |
} else if (hostElement.createShadowRoot) { | |
//Firefox Nightly: Go to `about:config`, enable `dom.webcomponents.enabled` | |
shadow = hostElement.createShadowRoot({mode: 'open'}); | |
} | |
shadow.innerHTML = `<span>red span</span> | |
<style>span { color: red; }</style>`; | |
</script> | |
<span>green span </span> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM
https://www.polymer-project.org/