class Counter extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
const text = this.shadowRoot.appendChild(document.createElement("p"));
const button = this.shadowRoot.appendChild(document.createElement("button"));
text.textContent = 0;
button.textContent = "add";
button.addEventListener("click", () => {
text.textContent = Number(text.textContent) + 1;
});
}
}
customElements.define("number-counter", Counter);
<number-counter></number-counter>
class Counter extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: "open" });
const text = this.shadowRoot.appendChild(document.createElement("p"));
const button = this.shadowRoot.appendChild(document.createElement("button"));
text.textContent = 0;
button.textContent = "add";
button.addEventListener("click", () => {
text.textContent = Number(text.textContent) + 1;
});
}
}
customElements.define("number-counter", Counter);
<number-counter></number-counter>
Bu şekilde yaptım.
JavaScript:class Counter extends HTMLElement { connectedCallback() { this.attachShadow({ mode: "open" }); const text = this.shadowRoot.appendChild(document.createElement("p")); const button = this.shadowRoot.appendChild(document.createElement("button")); text.textContent = 0; button.textContent = "add"; button.addEventListener("click", () => { text.textContent = Number(text.textContent) + 1; }); } } customElements.define("number-counter", Counter);
HTML:<number-counter></number-counter>
This. Attachshadow({ mode: "open" });
Evet.Bu kodun amacı oluşturduğumuz elementi shadowroot ağacı altına eklemek için istek göndermek mi? Eğer closed olursa null değer mi dönüyor?
Bilmiyorum, bende bu zamana kadar kullanmadım sadece duymuştum.Teşekkür ederim. Bir de bu JS in biraz ileride konusu mu daha once hiçbir egitimde web component gormemistim de. Ve biraz zorlayici geldi ne yalani söyleyeyim.
Bilmiyorum, bende bu zamana kadar kullanmadım sadece duymuştum.
Hayır.Peki cok derinlemesine ogrenmesem. Bir şey kaybeder miyim?
Hayır.
Bu sitenin çalışmasını sağlamak için gerekli çerezleri ve deneyiminizi iyileştirmek için isteğe bağlı çerezleri kullanıyoruz.