作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 .popin-foto
元素中打开一个 popin。当我尝试在同一元素中打开子类 popin 时,它不起作用。
代码
这是 parent
function Popin(container, titulo, url_listagem) {
this.url_listagem = url_listagem;
this.titulo = titulo;
this.overlay = $(".popin-overlay");
this.closeButton = $(".popin-close");
this.container = container;
}
Popin.prototype.header = function() {
var dados = {titulo: this.titulo};
var html = $.tmpl("header", dados);
this.container.append(html);
};
Popin.prototype.body = function() {
var html = $.tmpl("body");
this.container.append(html);
};
Popin.prototype.footer = function() {
var html = $.tmpl("footer");
this.container.append(html);
};
Popin.prototype.close = function() {
var self = this;
this.container.hide(100,function(){
self.overlay.fadeOut('fast');
});
$(".popin-header").remove();
$(".popin-body").remove();
$(".popin-footer").remove();
};
Popin.prototype.open = function(){
var self = this;
this.header();
this.body();
this.footer();
this.closeButton.click(function(){
self.close();
});
this.overlay.fadeTo("fast", 0.8, function(){
self.container.show();
});
};
子类
function PopinFoto(){}
PopinFoto.prototype = new Popin($(".popin-fotos"), "fotos", "fake_url");
PopinFoto.prototype.open = function(){
Popin.prototype.open.call(this);
$(".enviar-foto").die().live('click', function(){
//do something
});
};
所以,我这样做:
var popin = new Popin($(".popin-foto"), "title", "link");
popin.open();
popin.close();
var popinFoto = new PopinFoto($(".popin-foto"), "title", "link");
popinFoto.open(); //this not works
popin.close();
并且在控制台中,没有出现错误。
你能帮帮我吗?
最佳答案
看起来您的子类没有正确设置,因为您正在将子类的原型(prototype)设置为父类(super class)的具体实例。
我不确定你到底想达到什么目的,但我敢打赌子类构造函数需要直接调用父类(super class)构造函数,就像这样:
function PopinFoto(container, titulo, url_listagem){
Popin.call(this, container, titulo, url_listagem);
}
关于Javascript 继承 : How to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7679089/
我是一名优秀的程序员,十分优秀!