Created
October 17, 2020 20:24
-
-
Save andreburto/bc27553f1e7c47b251edfe69bae27afc to your computer and use it in GitHub Desktop.
Shadow DOM tinkering
This file contains hidden or 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</title> | |
<script> | |
function addShadow() { | |
const theDiv = document.getElementById("theDiv2") | |
const shadowRoot = theDiv.attachShadow({mode: 'open'}) | |
const para = document.createElement("p") | |
para.textContent = "Hello, Again!" | |
shadowRoot.appendChild(para) | |
} | |
function colorGreen() { | |
document.body.querySelectorAll("p").forEach(function (el) { | |
el.style.color = "#00FF00" | |
}) | |
} | |
function shadowGreen() { | |
const theDiv = document.getElementById("theDiv2") | |
theDiv.shadowRoot.querySelectorAll("p").forEach(function (el) { | |
el.style.color = "#00CC00" | |
}) | |
} | |
</script> | |
</head> | |
<body onload="addShadow()"> | |
<div id="theDiv"><p>Hello, World!</p></div> | |
<div id="theDiv2"> | |
</div> | |
<button onclick="colorGreen()">Green</button> | |
<button onclick="shadowGreen()">Shadow Green</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment