4

请帮助我,我不明白到底是什么导致了错误。编码:-

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>
原以为它将取第一个方法的名称(此处为lol-foo)并添加类路径并定义它,但似乎写出了错误并传递了[object HTMLElement]。

您的帮助将帮助custags.js帮助添加像 jQuery($) 这样的选择器。答案表示赞赏。

4

1 回答 1

2

问题是因为您为 的第一个参数提供了一个 HTMLElement 对象define(),而它却期望自定义元素名称为字符串。作为参考,此名称必须包含连字符,并且小写。因此试试这个:

customElements.define(item.tagName.toLowerCase(), className);

这是一个工作示例:

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.tagName.toLowerCase(), 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>

还值得注意的是,第二个参数define()是类构造函数,所以参数名称className是用词不当。但是,在这个例子中它的值是正确的。

您可以在 MDN上找到更多信息CustomElementRegistry.define()

于 2019-09-29T09:50:39.760 回答