aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/api/customelementregistry/define/index.html
blob: b51ed46c37a578f1005ff3380806f4292caae934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
title: CustomElementRegistry.define()
slug: Web/API/CustomElementRegistry/define
tags:
  - API
  - CustomElementRegistry
  - Method
  - Reference
  - Web Components
  - Webkomponente
  - benutzerdefiniert
  - custom elements
  - define
translation_of: Web/API/CustomElementRegistry/define
---
<p>{{APIRef("CustomElementRegistry")}}</p>

<p>Die <code><strong>define()</strong></code> Methode der {{domxref("CustomElementRegistry")}} Schnittstelle beschreibt ein neues benutzerdefiniertes Element.</p>

<p>Es gibt zwei Arten von benutzerdefinierten Elementen, die erstellt werden können:</p>

<ul>
 <li><strong>Autonomes benutzerdefiniertes Element:</strong> Eigenständige Elemente; Sie erben nicht von eingebauten HTML-Elementen.</li>
 <li><strong>Angepasstes integriertes Element:</strong> Diese Elemente erben und erweitern eingebaute HTML-Elemente.</li>
</ul>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">customElements.define(<em>name</em>, <em>constructor</em>, <em>options</em>);
</pre>

<h3 id="Parameter">Parameter</h3>

<dl>
 <dt>name</dt>
 <dd>Name des neuen benutzerdefinierten Elements. Beachte, dass Namen von benutzerdefinierten Elementen einen Bindestrich enthalten müssen.</dd>
 <dt>constructor</dt>
 <dd>Konstruktor für das neue, benutzerdefinierte Element.</dd>
 <dt>options {{optional_inline}}</dt>
 <dd>Objekt, das steuert, wie das Element definiert ist. Eine Option wird derzeit unterstützt:
 <ul>
  <li><code>extends</code>: String des Namens eines integrierten Elements das erweitert werden soll. Wird verwendet um ein <em>angepasstes integriertes Element</em>  zu erzeugen<em>.</em></li>
 </ul>
 </dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>Void.</p>

<h3 id="Ausnahmebehandlung">Ausnahmebehandlung</h3>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>NotSupportedError</code></td>
   <td>
    <p>Das {{domxref("CustomElementRegistry")}} enthält bereits einen Eintrag mit dem gleichen Namen oder dem gleichen Konstruktor (oder ist auf andere Weise bereits definiert), oder <code>extends</code> ist angegeben und es ist ein <a href="https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name">gültiger benutzerdefinierter Elementname</a> oder <code>extends</code> ist angegeben, aber das Element, das es zu erweitern versucht, ist ein unbekanntes Element.</p>
   </td>
  </tr>
  <tr>
   <td><code>SyntaxError</code></td>
   <td>Der angegebene Name ist kein <a href="https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name">gültiger benutzerdefinierter Elementname</a>.</td>
  </tr>
  <tr>
   <td><code>TypeError</code></td>
   <td>Der referenzierte Konstruktor ist kein Konstruktor.</td>
  </tr>
 </tbody>
</table>

<div class="note">
<p><strong>Hinweis</strong>: Du wirst oftmals  <code>NotSupportedError</code>s geworfen bekommen, die so erscheinen als würde <code>define()</code> fehlschlagen, aber stattdessen wahrscheinlich ein Problem mit {domxref("Element.attachShadow()")}} sind.</p>
</div>

<h2 id="Beispiele">Beispiele</h2>

<h3 id="Autonomes_benutzerdefiniertes_Element">Autonomes benutzerdefiniertes Element</h3>

<p>Der folgende Code entstammt dem <a href="https://github.com/mdn/web-components-examples/tree/master/popup-info-box-web-component">popup-info-box-web-component</a> Beispiel (<a href="https://mdn.github.io/web-components-examples/popup-info-box-web-component/">siehe Live</a>).</p>

<pre class="brush: js">// Create a class for the element
class PopUpInfo extends HTMLElement {
  constructor() {
    // Always call super first in constructor
    super();

    // Create a shadow root
    var shadow = this.attachShadow({mode: 'open'});

    // Create spans
    var wrapper = document.createElement('span');
    wrapper.setAttribute('class','wrapper');
    var icon = document.createElement('span');
    icon.setAttribute('class','icon');
    icon.setAttribute('tabindex', 0);
    var info = document.createElement('span');
    info.setAttribute('class','info');

    // Take attribute content and put it inside the info span
    var text = this.getAttribute('text');
    info.textContent = text;

    // Insert icon
    var imgUrl;
    if(this.hasAttribute('img')) {
      imgUrl = this.getAttribute('img');
    } else {
      imgUrl = 'img/default.png';
    }
    var img = document.createElement('img');
    img.src = imgUrl;
    icon.appendChild(img);

    // Create some CSS to apply to the shadow dom
    var style = document.createElement('style');

    style.textContent = '.wrapper {' +
                           'position: relative;' +
                        '}' +

                         '.info {' +
                            'font-size: 0.8rem;' +
                            'width: 200px;' +
                            'display: inline-block;' +
                            'border: 1px solid black;' +
                            'padding: 10px;' +
                            'background: white;' +
                            'border-radius: 10px;' +
                            'opacity: 0;' +
                            'transition: 0.6s all;' +
                            'position: absolute;' +
                            'bottom: 20px;' +
                            'left: 10px;' +
                            'z-index: 3;' +
                          '}' +

                          'img {' +
                            'width: 1.2rem' +
                          '}' +

                          '.icon:hover + .info, .icon:focus + .info {' +
                            'opacity: 1;' +
                          '}';

    // attach the created elements to the shadow dom

    shadow.appendChild(style);
    shadow.appendChild(wrapper);
    wrapper.appendChild(icon);
    wrapper.appendChild(info);
  }
}

// Define the new element
customElements.define('popup-info', PopUpInfo);
</pre>

<pre class="brush: html">&lt;popup-info img="img/alt.png" text="Your card validation code (CVC) is an extra
                                    security feature — it is the last 3 or 4
                                    numbers on the back of your card."&gt;</pre>

<div class="note">
<p><strong>Hinweis</strong>: Konstruktoren für autonome benutzerdefinierte Elemente müssen {{domxref("HTMLElement")}} extenden.</p>
</div>

<h3 id="Benutzerdefiniertes_integriertes_Element">Benutzerdefiniertes integriertes Element</h3>

<p>Der folgende Code entstammt dem <a href="https://github.com/mdn/web-components-examples/tree/master/word-count-web-component">word-count-web-component</a> Beispiel (<a href="https://mdn.github.io/web-components-examples/word-count-web-component/">siehe Live</a>).</p>

<pre class="brush: js">// Create a class for the element
class WordCount extends HTMLParagraphElement {
  constructor() {
    // Always call super first in constructor
    super();

    // count words in element's parent element
    var wcParent = this.parentNode;

    function countWords(node){
      var text = node.innerText || node.textContent
      return text.split(/\s+/g).length;
    }

    var count = 'Words: ' + countWords(wcParent);

    // Create a shadow root
    var shadow = this.attachShadow({mode: 'open'});

    // Create text node and add word count to it
    var text = document.createElement('span');
    text.textContent = count;

    // Append it to the shadow root
    shadow.appendChild(text);


    // Update count when element content changes
    setInterval(function() {
      var count = 'Words: ' + countWords(wcParent);
      text.textContent = count;
    }, 200)

  }
}

// Define the new element
customElements.define('word-count', WordCount, { extends: 'p' });</pre>

<pre class="brush: html">&lt;p is="word-count"&gt;&lt;/p&gt;</pre>

<h2 id="Spezifikationen">Spezifikationen</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spezifikation</th>
   <th scope="col">Status</th>
   <th scope="col">Kommentar</th>
  </tr>
  <tr>
   <td>{{SpecName("HTML WHATWG", "custom-elements.html#dom-customelementregistry-define", "customElements.define()")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browserkompatibilität">Browserkompatibilität</h2>

<div>


<p>{{Compat("api.CustomElementRegistry.define")}}</p>
</div>