我正在加载一个js文件来构建一个表单(我知道这不是标准的;这只是为了我的学习)。该文件的内容如下所示:
window.captor = (function () {
function Captor () {
}
var captor = {
create: function () {
return new Captor();
}
};
Captor.prototype.build = function (json) {
//CREATE BACKDROP WITH CLOSE FUNCTION
var interstitial = this.backdrop();
//CREATE DIV CONTAINER
var container = this.container();
//CREATE TITLE
var title = this.setTitle();
//CREATE CALL TO ACTION
var cta = this.callToAction();
//CREATE FORM WITH INPUTS
var form = this.capture(json);
interstitial.appendChild(container);
container.appendChild(title);
container.appendChild(cta);
container.appendChild(form);
document.body.parentNode.appendChild(interstitial);
};
Captor.prototype.backdrop = function(){
var b = document.createElement("div");
b.setAttribute('style',"position:fixed;width:100%;height:100%;background: rgba(0,0,0,.6);z-index: 100;");
return b;
};
Captor.prototype.container = function(){
var c = document.createElement("div");
c.setAttribute('style',"position:relative;background:#dedede;min-height:100px;width: 480px;margin: auto;top: 45%;-webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px;");
return c;
};
Captor.prototype.setTitle = function(){
var t = document.createElement("h1");
var inside = document.createTextNode("Join Our Newsletter");
t.appendChild(inside);
return t;
};
Captor.prototype.callToAction = function(){
var cta = document.createElement("p");
var inside = document.createTextNode("Would you like to find out more about stuff? Join our newsletter!");
cta.appendChild(inside);
return cta;
};
Captor.prototype.capture = function(json){
var f = document.createElement("form");
var hidden = document.createElement("input");
hidden.setAttribute('type',"hidden");
f.appendChild(hidden);
return f;
};
return captor;
}());
然后在包含该文件后,我打电话给:
var n = captor.create();
n.build(window.builder.getj);
window.builder.getj
返回一个json对象(我还没有对该obj做过任何事情)。
到目前为止,表单还没有构建。我不确定是否有任何东西被调用。当我查看控制台时,没有输出任何内容。如果我致电var c = captor.create();
,则会返回undefined
。如果我运行c = captor.create();
,则返回Captor {build: function, backdrop: function, container: function, setTitle: function, callToAction: function…}
。
我觉得我必须遗漏一些东西,但对于我的生活,我看不出它是什么。如何以正确的方式呈现此表单?