Ответ 1
Атрибут is
должен использоваться, если вы расширяете другой стандартный элемент HTML. Поскольку элемент "checkbox" отсутствует, вы не можете расширять такие объекты, как input[type=checkbox]
или radio
. Расширьте элемент input
(HTMLInputElement
) и укажите тип в createdCallback
:
<body>
<input is="totally-not-checkbox"> <!-- Custom Checkbox #1 -->
</body>
var proto = Object.create(HTMLInputElement.prototype);
proto.createdCallback = function() {
this.type = "checkbox";
this.addEventListener("change", this.toggleZoom);
};
proto.toggleZoom = function(){
$(this).toggleClass("zoom"); //FYI $ is jQuery
};
var nCB = document.registerElement("totally-not-checkbox", {
prototype: proto,
extends: 'input'
});
document.body.appendChild(new nCB()); //Custom Checkbox #2
Демо: http://jsfiddle.net/DerekL/5utco0en/
Поскольку пользовательский элемент простирается от HTMLInputElement
, все исходные свойства и методы наследуются. Взгляните на эту демонстрацию: http://jsfiddle.net/DerekL/6gtLp8p5/
proto.toggleZoom = function(e){
$(this).toggleClass("zoom");
console.log(this, this.checked);
};
this.checked
вернет правильное значение checked
, как и исходное.