请帮助我,我不明白到底是什么导致了错误。编码:-
class button extends HTMLElement{
constructor(){
super();
const shadow = this.attachShadow({mode: 'open'});
const button = document.createElement('button');
button.style.cssText = 'display:block';
button.textContent = super.textContent;
shadow.appendChild(button);
}
}
var Ω = (function () {
'use strict';
/**
* Create the constructor
* @param {String} selector The selector to use
*/
var Constructor = function (selector) {
if (!selector) return;
if (selector === 'document') {
this.elems = [document];
} else if (selector === 'window') {
this.elems = [window];
} else {
this.elems = document.querySelectorAll(selector);
}
};
/**
* Run a callback on each item
* @param {Function} callback The callback function to run
*/
Constructor.prototype.each = function (callback) {
if (!callback || typeof callback !== 'function') return;
for (var i = 0; i < this.elems.length; i++) {
callback(this.elems[i], i);
}
return this;
};
/**
*
* @param {String} className
*/
Constructor.prototype.register = function (className) {
this.each(function (item) {
customElements.define(item, className);
});
return this;
};
/**
* Instantiate a new constructor
*/
var instantiate = function (selector) {
return new Constructor(selector);
};
/**
* Return the constructor instantiation
*/
return instantiate;
})();
Ω('lol-foo').register(button);
<p>Expect a button down below</p>
<lol-foo>Hey</lol-foo>
您的帮助将帮助custags.js帮助添加像 jQuery($) 这样的选择器。答案表示赞赏。