로고

(주)대도
로그인 회원가입
  • 자유게시판
  • 자유게시판

    자유게시판

    Apa Itu Shadow DOM?

    페이지 정보

    profile_image
    작성자 Jacob
    댓글 0건 조회 14회 작성일 24-09-01 12:24

    본문

    <div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Shadow DOM dapat mengisolasi sebagian struktur DOM di dalam komponen sehingga tidak dapat disentuh dari luar komponen atau nodenya. Singkatnya kita bisa sebut Shadow DOM sebagai "DOM dalam DOM". Bagaimana ia bekerja? Perhatikan ilustrasi berikut:</div><a name='more'></a><br /><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><span class="fr-img-caption fr-fic fr-dib" style="box-sizing: border-box; display: block; float: none; margin: 5px auto; max-width: 100%; position: relative; text-align: center; vertical-align: top; width: 873px;"><span class="fr-img-wrap" style="box-sizing: border-box; display: inline-block; margin: auto; padding: 0px; width: 757px;"><img alt="2020031020401056fdb6ae992399a36fcbfa50e75cf4ee.png" src="https://dicodingacademy.blob.core.windows.net/academies/2020031020401056fdb6ae992399a36fcbfa50e75cf4ee.png" style="border-style: none; box-sizing: border-box; display: block; margin: auto; max-width: 100%; position: relative; vertical-align: middle;" /><span class="fr-inner" style="box-sizing: border-box; display: block; font-size: 14px; font-weight: initial; margin: auto; opacity: 0.9; padding: 5px 5px 10px; width: 757px;">Sumber:&nbsp;<a href="https://www.google.com/url?q=https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM&amp;sa=D&amp;ust=1583841656284000" rel="noreferrer noopener" style="background-color: transparent; box-sizing: border-box; color: rgb(255, 84, 131) !important; text-decoration-line: none;" target="_blank">https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM</a></span></span></span></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Shadow DOM dapat membuat DOM Tree lain terbentuk secara terisolasi melalui host yang merupakan komponen dari regular DOM Tree (Document Tree). Shadow DOM Tree ini dimulai dari root bayangan (Shadow root), yang dibawahnya dapat memiliki banyak element lagi layaknya Document Tree.</div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Terdapat beberapa terminologi yang perlu kita ketahui dari ilustrasi di atas:</div><ul style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem; margin-top: 0px;"><li dir="ltr" style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;">Shadow host</em></span>&nbsp;: Merupakan komponen/node yang terdapat pada regular DOM di mana shadow DOM terlampir pada komponen/node ini.</li><li dir="ltr" style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;">Shadow tree&nbsp;</em></span>: DOM Tree di dalam shadow DOM.</li><li dir="ltr" style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;">Shadow boundary</em></span>&nbsp;: Batas dari shadow DOM dengan regular DOM.</li><li dir="ltr" style="box-sizing: border-box;"><div dir="ltr" style="box-sizing: border-box; margin-bottom: 1rem;"><span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;">Shadow root</em></span>&nbsp;: Root node dari shadow tree.</div></li></ul><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Kita dapat memanipulasi elemen yang terdapat di dalam shadow tree layaknya pada document tree, namun cakupannya selama kita berada di dalam shadow boundary. Dengan kata lain, jika kita berada di document tree kita tidak dapat memanipulasi elemen bahkan menerapkan styling pada elemen yang terdapat di dalam shadow tree. Itulah mengapa shadow DOM dapat membuat komponen terenkapsulasi</div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Untuk melampirkan Shadow DOM pada elemen penggunaan sangat mudah, yaitu dengan menggunakan properti&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">attachShadow</span></em></span>&nbsp;pada elemen-nya seperti ini:</div><div class="panel panel-default panel-code" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><div class="panel-heading bg-light" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box;"><ul class="nav nav-pills pl-2 pr-2" role="tablist" style="box-sizing: border-box; display: flex; flex-wrap: wrap; list-style: none; margin-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0.5rem !important;"><li class="nav-item" style="box-sizing: border-box; cursor: pointer;"><a aria-selected="true" class="nav-link active show" data-toggle="pill" href="https://www.dicoding.com/academies/163/tutorials/7057?from=7050#tab1-code1" role="tab" style="background: 0px 0px; border-bottom: 4px solid rgb(44, 62, 80); border-radius: 0px; box-sizing: border-box; color: rgb(44, 62, 80) !important; display: block; font-weight: 700; padding: 0.5rem; text-decoration-line: none;">main.js</a></li><li class="nav-item" style="box-sizing: border-box; cursor: pointer;"><br /></li></ul></div><div class="panel-body bg-light p-2" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box; padding: 0.5rem !important;"><div class="tab-content" style="box-sizing: border-box;"><div class="tab-pane show active" id="tab1-code1" role="tabpanel" style="box-sizing: border-box;"><pre class="prettyprint linenums prettyprinted" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// Shadow Host</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;divElement </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;document</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">createElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="str" style="box-sizing: border-box; color: #008800;">"div"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="com" style="box-sizing: border-box; color: #880000;">// element yang berada di dalam Shadow DOM</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;headingElement </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;document</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">createElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="str" style="box-sizing: border-box; color: #008800;">"h1"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">headingElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">innerText </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="str" style="box-sizing: border-box; color: #008800;">"Ini merupakan konten di dalam shadow DOM"</span><span class="pun" style="box-sizing: border-box; color: #666600;">;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L8" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L9" style="background: rgb(238, 238, 238); box-sizing: border-box;"><span class="com" style="box-sizing: border-box; color: #880000;">// Melampirkan shadow root pada shadow host</span></li><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// Mengatur mode shadow dengan nilai open</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;shadowRoot </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;"> divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">attachShadow</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">mode</span><span class="pun" style="box-sizing: border-box; color: #666600;">:</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="str" style="box-sizing: border-box; color: #008800;">"open"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="com" style="box-sizing: border-box; color: #880000;">// Memasukkan element heading ke dalam shadow root</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">shadowRoot</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">appendChild</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">headingElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L8" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// Memasukkan elemen shadow host ke regular DOM</span></li><br /><li class="L9" style="background: rgb(238, 238, 238); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">document</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">body</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">appendChild</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /></ol><br /></pre></div></div></div></div><div class="panel-heading bg-light" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px;"><ul class="nav nav-pills pl-2 pr-2" role="tablist" style="box-sizing: border-box; display: flex; flex-wrap: wrap; list-style: none; margin-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0.5rem !important;"><li class="nav-item" style="box-sizing: border-box; cursor: pointer;"><a aria-selected="true" class="nav-link show active" data-toggle="pill" href="https://www.dicoding.com/academies/163/tutorials/7057?from=7050#tab1-code2" role="tab" style="background: 0px 0px; border-bottom: 4px solid rgb(44, 62, 80); border-radius: 0px; box-sizing: border-box; color: rgb(44, 62, 80) !important; display: block; font-weight: 700; padding: 0.5rem; text-decoration-line: none;">index.html</a></li></ul></div><div class="panel-body bg-light p-2" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; padding: 0.5rem !important;"><div class="tab-content" style="box-sizing: border-box;"><div class="tab-pane show active" id="tab1-code2" role="tabpanel" style="box-sizing: border-box;"><pre class="prettyprint linenums prettyprinted" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="dec" style="box-sizing: border-box; color: #660066;">&lt;!DOCTYPE html&gt;</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;html&gt;</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;head&gt;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;meta</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">charset</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"utf-8"</span><span class="tag" style="box-sizing: border-box; color: #000088;">&gt;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;meta</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">name</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"viewport"</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">content</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"width=device-width"</span><span class="tag" style="box-sizing: border-box; color: #000088;">&gt;</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;title&gt;</span><span class="pln" style="box-sizing: border-box; color: black;">Shadow DOM Basic Usage</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/title&gt;</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/head&gt;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;body&gt;</span></li><br /><li class="L8" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;script</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">src</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"main.js"</span><span class="tag" style="box-sizing: border-box; color: #000088;">&gt;&lt;/script&gt;</span></li><br /><li class="L9" style="background: rgb(238, 238, 238); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/body&gt;</span></li><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/html&gt;</span></li><br /></ol><br /></pre></div></div></div><div style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Jika kita lihat pada browser, maka struktur HTML yang akan dihasilkan adalah seperti ini:</div><div style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><br class="Apple-interchange-newline" /><img alt="2020031020442138d22cf8f4814aacc795f563c3015892.png" class="fr-fic fr-dib" src="https://dicodingacademy.blob.core.windows.net/academies/2020031020442138d22cf8f4814aacc795f563c3015892.png" style="border-style: none; box-sizing: border-box; display: block; float: none; margin: 5px auto; max-width: 100%; position: relative; vertical-align: top;" /></div><div style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Dan struktur DOM tree yang terbentuk akan tampak seperti ini:</div><div style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><img alt="20200310204452a9e4788ac5f7f206c20debfa0bc87524.png" class="fr-fic fr-dib" src="https://dicodingacademy.blob.core.windows.net/academies/20200310204452a9e4788ac5f7f206c20debfa0bc87524.png" style="border-style: none; box-sizing: border-box; display: block; float: none; margin: 5px auto; max-width: 100%; position: relative; vertical-align: top;" /></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Dalam penggunaan&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">attachShadow()</span></em></span>&nbsp;kita melampirkan objek dengan properti&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">mode</span></em></span>&nbsp;yang memiliki nilai ‘<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">open</span></em></span>’. Sebenarnya terdapat dua opsi nilai yang dapat digunakan dalam properti mode, yaitu "<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">open</span></em></span>" dan "<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">closed</span></em></span>".&nbsp;</div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Menggunakan nilai open berarti kita memperbolehkan untuk mengakses properti&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">shadowRoot</span></em></span>&nbsp;melalui elemen yang melampirkan Shadow DOM.&nbsp;</div><pre class="prettyprint linenums prettyprinted" dir="ltr" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; margin-bottom: 10px; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">attachShadow</span><span class="pun" style="box-sizing: border-box; color: #666600;">;</span></li><br /></ol><br /></pre><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">properti shadowRoot mengembalikan struktur DOM yang berada pada shadow tree.</div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><img alt="20200310204643a7a1f999761eaf74f9476e50013bb373.gif" class="fr-fic fr-dib" src="https://dicodingacademy.blob.core.windows.net/academies/20200310204643a7a1f999761eaf74f9476e50013bb373.gif" style="border-style: none; box-sizing: border-box; display: block; float: none; margin: 5px auto; max-width: 100%; position: relative; vertical-align: top;" /></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Namun jika kita menggunakan nilai&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">closed</span></em></span>&nbsp;maka properti&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">shadowRoot</span></em></span>&nbsp;akan mengembalikan nilai&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">null</span></em></span>.&nbsp;</div><pre class="prettyprint linenums prettyprinted" dir="ltr" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; margin-bottom: 10px; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;"> shadowRoot </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;"> divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">attachShadow</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">mode</span><span class="pun" style="box-sizing: border-box; color: #666600;">:</span><span class="pln" style="box-sizing: border-box; color: black;"> </span><span class="str" style="box-sizing: border-box; color: #008800;">"closed"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">shadowRoot </span><span class="com" style="box-sizing: border-box; color: #880000;">// null;</span></li><br /></ol><br /></pre><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Hal ini berarti kita sama sekali tidak dapat mengakses Shadow Tree selain melalui variabel yang kita definisikan ketika melampirkan Shadow DOM.</div><pre class="prettyprint linenums prettyprinted" dir="ltr" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; margin-bottom: 10px; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;"> shadowRoot </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;"> divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">attachShadow</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">mode</span><span class="pun" style="box-sizing: border-box; color: #666600;">:</span><span class="pln" style="box-sizing: border-box; color: black;"> </span><span class="str" style="box-sizing: border-box; color: #008800;">"closed"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">shadowRoot&nbsp;</span><span class="com" style="box-sizing: border-box; color: #880000;">// null;</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">shadowRoot </span><span class="com" style="box-sizing: border-box; color: #880000;">// # shadow-root (closed)</span></li><br /></ol><br /></pre><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><img alt="2020031020483296ef7fce493fd7c0528697c3fa03565b.gif" class="fr-fic fr-dib" src="https://dicodingacademy.blob.core.windows.net/academies/2020031020483296ef7fce493fd7c0528697c3fa03565b.gif" style="border-style: none; box-sizing: border-box; display: block; float: none; margin: 5px auto; max-width: 100%; position: relative; vertical-align: top;" /></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Karena Shadow DOM terisolasi dari document tree maka element yang terdapat di dalamnya pun tidak akan terpengaruh oleh styling yang berada diluar dari shadow root-nya.</div><div class="panel panel-default panel-code" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><div class="panel-heading bg-light" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box;"><ul class="nav nav-pills pl-2 pr-2" role="tablist" style="box-sizing: border-box; display: flex; flex-wrap: wrap; list-style: none; margin-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0.5rem !important;"><li class="nav-item" style="box-sizing: border-box; cursor: pointer;"><a class="nav-link active" data-toggle="pill" href="https://www.dicoding.com/academies/163/tutorials/7057?from=7050#tab2-code1" role="tab" style="background: 0px 0px; border-bottom: 4px solid rgb(44, 62, 80); border-radius: 0px; box-sizing: border-box; color: rgb(44, 62, 80) !important; display: block; font-weight: 700; padding: 0.5rem; text-decoration-line: none;"><br class="Apple-interchange-newline" />index.html</a></li><li class="nav-item" style="box-sizing: border-box; cursor: pointer;"><br /></li></ul></div><div class="panel-body bg-light p-2" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box; padding: 0.5rem !important;"><div class="tab-content" style="box-sizing: border-box;"><div class="tab-pane show active" id="tab2-code1" role="tabpanel" style="box-sizing: border-box;"><pre class="prettyprint linenums prettyprinted" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="dec" style="box-sizing: border-box; color: #660066;">&lt;!DOCTYPE html&gt;</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;html&gt;</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;head&gt;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;meta</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">charset</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"utf-8"</span><span class="tag" style="box-sizing: border-box; color: #000088;">&gt;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;meta</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">name</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"viewport"</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">content</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"width=device-width"</span><span class="tag" style="box-sizing: border-box; color: #000088;">&gt;</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;title&gt;</span><span class="pln" style="box-sizing: border-box; color: black;">Shadow DOM Basic Usage</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/title&gt;</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;style&gt;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp; &nbsp; &nbsp;h1&nbsp;</span><span class="pun" style="box-sizing: border-box; color: #666600;"></span></li><br /><li class="L8" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color</span><span class="pun" style="box-sizing: border-box; color: #666600;">:</span><span class="pln" style="box-sizing: border-box; color: black;"> red</span><span class="pun" style="box-sizing: border-box; color: #666600;">;</span></li><br /><li class="L9" style="background: rgb(238, 238, 238); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp; &nbsp; &nbsp;</span><span class="pun" style="box-sizing: border-box; color: #666600;"></span></li><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/style&gt;</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/head&gt;</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;body&gt;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;h1&gt;</span><span class="pln" style="box-sizing: border-box; color: black;">Ini merupakan konten yang berada di Document tree</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/h1&gt;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp; &nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;script</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="atn" style="box-sizing: border-box; color: #660066;">src</span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="atv" style="box-sizing: border-box; color: #008800;">"main.js"</span><span class="tag" style="box-sizing: border-box; color: #000088;">&gt;&lt;/script&gt;</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/body&gt;</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="tag" style="box-sizing: border-box; color: #000088;">&lt;/html&gt;</span></li><br /></ol><br /></pre></div></div></div></div><div class="panel-heading bg-light" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px;"><ul class="nav nav-pills pl-2 pr-2" role="tablist" style="box-sizing: border-box; display: flex; flex-wrap: wrap; list-style: none; margin-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0.5rem !important;"><li class="nav-item" style="box-sizing: border-box; cursor: pointer;"><a aria-selected="true" class="nav-link active show" data-toggle="pill" href="https://www.dicoding.com/academies/163/tutorials/7057?from=7050#tab2-code2" role="tab" style="background: 0px 0px; border-bottom: 4px solid rgb(44, 62, 80); border-radius: 0px; box-sizing: border-box; color: rgb(44, 62, 80) !important; display: block; font-weight: 700; padding: 0.5rem; text-decoration-line: none;">main.js</a></li></ul></div><div class="panel-body bg-light p-2" style="background-color: rgb(248, 249, 250) !important; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; padding: 0.5rem !important;"><div class="tab-content" style="box-sizing: border-box;"><div class="tab-pane active show" id="tab2-code2" role="tabpanel" style="box-sizing: border-box;"><pre class="prettyprint linenums prettyprinted" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// Shadow Host</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;divElement </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;document</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">createElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="str" style="box-sizing: border-box; color: #008800;">"div"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="com" style="box-sizing: border-box; color: #880000;">// element yang berada di dalam Shadow DOM</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;headingElement </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;document</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">createElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="str" style="box-sizing: border-box; color: #008800;">"h1"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">headingElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">innerText </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="str" style="box-sizing: border-box; color: #008800;">"Ini merupakan konten di dalam shadow DOM"</span><span class="pun" style="box-sizing: border-box; color: #666600;">;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L8" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L9" style="background: rgb(238, 238, 238); box-sizing: border-box;"><span class="com" style="box-sizing: border-box; color: #880000;">// Melampirkan shadow root pada shadow host</span></li><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// Mengatur mode shadow dengan nilai open</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="kwd" style="box-sizing: border-box; color: #000088;">const</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;shadowRoot </span><span class="pun" style="box-sizing: border-box; color: #666600;">=</span><span class="pln" style="box-sizing: border-box; color: black;"> divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">attachShadow</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">mode</span><span class="pun" style="box-sizing: border-box; color: #666600;">:</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="str" style="box-sizing: border-box; color: #008800;">"open"</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="com" style="box-sizing: border-box; color: #880000;">// Memasukkan element heading ke dalam shadow root</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">shadowRoot</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">appendChild</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">headingElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span></li><br /><li class="L8" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// Memasukkan elemen shadow host ke regular DOM</span></li><br /><li class="L9" style="background: rgb(238, 238, 238); box-sizing: border-box;"><span class="pln" style="box-sizing: border-box; color: black;">document</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">body</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">appendChild</span><span class="pun" style="box-sizing: border-box; color: #666600;">(</span><span class="pln" style="box-sizing: border-box; color: black;">divElement</span><span class="pun" style="box-sizing: border-box; color: #666600;">);</span></li><br /></ol><br /></pre></div></div></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><br /></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Jika dilihat pada browser maka hasilnya akan seperti ini:</div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;"><br class="Apple-interchange-newline" /><img alt="20200310205023649403bfa8d61e879afc65f4a050c502.png" class="fr-fic fr-dib" src="https://dicodingacademy.blob.core.windows.net/academies/20200310205023649403bfa8d61e879afc65f4a050c502.png" style="border-style: none; box-sizing: border-box; display: block; float: none; margin: 5px auto; max-width: 100%; position: relative; vertical-align: top;" /></div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Berdasarkan hasil di atas, styling hanya akan diterapkan pada elemen&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">&lt;h1&gt;</span></em></span>&nbsp;yang berada di document tree. Sedangkan elemen&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">&lt;h1&gt;</span></em></span>&nbsp;yang berada pada shadow dom tidak akan terpengaruh dengan styling tersebut. Lantas, bagaimana caranya kita melakukan styling pada Shadow DOM?</div><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Kita dapat melakukannya dengan menambahkan template&nbsp;<span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">&lt;style&gt;</span></em></span>&nbsp;di dalam&nbsp;<span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;"><span style="box-sizing: border-box; font-weight: 700;"><em style="box-sizing: border-box;">shadowRoot.innerHTML</em></span></span>. &nbsp;Contohnya seperti ini:</div><pre class="prettyprint linenums prettyprinted" dir="ltr" style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(136, 136, 136); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.8px; line-height: unset; margin-bottom: 10px; max-height: 500px; overflow-wrap: break-word; overflow: visible auto; padding: 2px; white-space: pre-wrap; word-break: break-all;"><ol class="linenums" style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px; min-width: max-content;"><br /><li class="L0" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="com" style="box-sizing: border-box; color: #880000;">// menetapkan styling pada Shadow DOM</span></li><br /><li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="pln" style="box-sizing: border-box; color: black;">shadowRoot</span><span class="pun" style="box-sizing: border-box; color: #666600;">.</span><span class="pln" style="box-sizing: border-box; color: black;">innerHTML </span><span class="pun" style="box-sizing: border-box; color: #666600;">+=</span><span class="pln" style="box-sizing: border-box; color: black;">&nbsp;</span><span class="str" style="box-sizing: border-box; color: #008800;">`</span></li><br /><li class="L2" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="str" style="box-sizing: border-box; color: #008800;">&nbsp;&lt;style&gt;</span></li><br /><li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="str" style="box-sizing: border-box; color: #008800;">&nbsp; &nbsp;h1 </span></li><br /><li class="L4" style="background: rgb(255, 255, 255); box-sizing: border-box;"><span class="str" style="box-sizing: border-box; color: #008800;">&nbsp; &nbsp; &nbsp;color: Tukang Aluminium green;</span></li><br /><li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="str" style="box-sizing: border-box; color: #008800;">&nbsp; &nbsp;</span></li><br /><li class="L6" style="background: rgb(255, 255, 255); box-sizing: border-box; list-style-type: none;"><span class="str" style="box-sizing: border-box; color: #008800;">&nbsp;&lt;/style&gt;</span></li><br /><li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; list-style-type: none;"><span class="str" style="box-sizing: border-box; color: #008800;">`</span><span class="pun" style="box-sizing: border-box; color: #666600;">;</span></li><br /></ol><br /></pre><div dir="ltr" style="background-color: white; box-sizing: border-box; color: #3d3d3d; font-family: Quicksand, sans-serif; font-size: 16px; margin-bottom: 1rem;">Maka element&nbsp;<em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;"><span style="background-color: #f9f2f4; box-sizing: border-box; color: #c7254e; padding: 2px 4px;">&lt;style&gt;</span></span></em>&nbsp;tersebut akan bera

    댓글목록

    등록된 댓글이 없습니다.