2016-09-11 4 views
3

Es ist möglich benutzerdefinierte Funktionen für benutzerdefinierte Elemente zu definieren?Benutzerdefinierte Methoden von Webkomponenten

Etwas wie:

var proto = Object.create(HTMLElement.prototype); 
proto.customMethod = function() { ... }; 

document.registerElement('custom-el', { 
    prototype: proto 
}); 

und das Verfahren auf dem Element Aufruf:

var istance = document.createElement('custom-el'); 
instance.customMethod(); 

Antwort

2

Ja, natürlich.

Ihr Beispiel funktioniert, wie Sie in der unten stehenden Code-Schnipsel sehen:

var proto = Object.create(HTMLElement.prototype); 
 
proto.customMethod = function() { 
 
    console.log('customMethod called') 
 
}; 
 

 
document.registerElement('custom-el', { 
 
    prototype: proto 
 
}); 
 
var instance = document.createElement('custom-el'); 
 
instance.customMethod();

Verwandte Themen