【问题标题】:Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': "[object HTMLElement]" is not a valid custom element name未捕获的 DOMException:无法在“CustomElementRegistry”上执行“定义”:“[object HTMLElement]”不是有效的自定义元素名称
【发布时间】:2019-09-29 09:08:39
【问题描述】:

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

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($) 这样的选择器。 感谢您的回答。

【问题讨论】:

    标签: javascript jquery dom customization domexception


    【解决方案1】:

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

    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的参数名称用词不当。不过,在这个例子中它的值是正确的。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多