🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 3 Jahren 2 Min Lesezeit
0

Web Components:Observed Attributes

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Image description



Notice the Vikings link background is Purple. The other one is Yellow.



The HTML markup for the image above was:




CODE
<h2>
The NFC North
</h2>
<a href="https://www.vikings.com/">
<color-span bgc="Purple" >
Vikings
</color-span>
</a>,
<a href="#">
<color-span>
Green Bay
</color-span>
</a>
, and others...










Observed Attributes



Web Components may use a static getter for observedAttributes(), like this:




CODE
static get observedAttributes() {
return ["bgc"];
}






Notice we define a variable named "bgc" for BackGroundColor.






The Attribute Changed CallBack



Because of the defined static getter function observedAttributes(), the WebComponent interface will call the attributeChanedCallback function when an attribute changes.




CODE
attributeChangedCallback(name, oldValue, newValue) {  
this.SetStyle(name,newValue);
}










SetStyle Function






CODE
SetStyle(name, color){
let backgroundColor = "Yellow";
if(color=="Purple"){
backgroundColor="#C5B4E3";
}
this.style.textContent = `
span:hover { text-decoration: underline; color:red; }
:host(.footer) { color : #555; }
:host { background-color:
${backgroundColor}; padding: 2px 5px; }
`
;
}









The Color Span Web Component






CODE
class ColorSpan extends HTMLElement {
span = null;
style = null;

constructor() {
super();

this.span = document.createElement("span");
this.span.textContent = this.textContent;
this.style = document.createElement("style");
this.SetStyle();

let shadowRoot = this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(this.style);
shadowRoot.appendChild(this.span);
}
SetStyle(name, color) {
let backgroundColor = "Yellow";
if (color == "Purple") {
backgroundColor = "#C5B4E3";
}
this.style.textContent = `
span:hover { text-decoration: underline; color:red; }
:host(.footer) { color : #555; }
:host { background-color:
${backgroundColor}; padding: 2px 5px; }
`
;
}
static get observedAttributes() {
return ["bgc", "color"];
}

attributeChangedCallback(name, oldValue, newValue) {
this.SetStyle(name, newValue);
}
}

// Define the new element
customElements.define("color-span", ColorSpan);










The HTML Head



The script tag brings in the component from the main.js file with the defer attribute.




CODE
  <head>
<meta charset="utf-8">
<title>Host selectors</title>
<script src="main.js" defer></script>
<link rel="stylesheet" href="styles.css">
</head>









Warning



Most purists would say this is a violation of concerns. And they would be right. We already have Styles, so why do this? The answer is to learn how a Web Component callback works.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Web Components:Observed Attributes

Thematisch verwandte Begriffe: ComponentsObserved, Attributes · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...