JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 从(扩展)HTML元素的子类化?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我喜欢它,如果我可以创建自己的类来扩展 HTML元素类,例如 HTMLDivElement或HTML ImageElement.

假设标准继承模式:

// class A:

function ClassA(foo) {
    this.foo = foo;
}

ClassA.prototype.toString = function() {
    return "My foo is " + this.foo;
};

// class B:

function ClassB(foo,bar) {
    ClassA.call(this,foo);
    this.bar = bar;
}

ClassB.prototype = new ClassA();

ClassB.prototype.toString = function() {
    return "My foo/bar is " + this.foo + "/" + this.bar;
};

我无法弄清楚构造函数中要调用的内容:

function Image2() {
    // this doesn't work:
    Image2.prototype.constructor.call(this);
    // neither does this (only applies to <img>,no equivalent for <span> etc.):
    Image.call(this);
}

Image2.prototype = new Image(); // or document.createElement("img");

Image2.prototype.toString = function() {
    return "My src is " + this.src;
};

是不可能的吗?还是有某种方法?谢谢. =)

解决方法

好的,这个问题需要从当前技术的更新.

HTML5提供了创建自定义元素的方法.您需要通过document.registerElement注册您的元素,之后您可以像使用任何其他标记一样使用它.该功能已在最新的Chrome中提供.我不确定其他浏览器.

更多细节在这里:

http://www.html5rocks.com/en/tutorials/webcomponents/customelements/

大佬总结

以上是大佬教程为你收集整理的javascript – 从(扩展)HTML元素的子类化?全部内容,希望文章能够帮你解决javascript – 从(扩展)HTML元素的子类化?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。