[英]Modify a DOM element inside a function
I have this task.我有这个任务。 Using the knowledge about the Factory method pattern, implement the SectionCreator factory which allows to create
(create(type) method)
two types of programs:使用有关工厂方法模式的知识,实现允许创建
(create(type) method)
两种类型的程序的 SectionCreator 工厂:
I created a join-us-section.js where i have this code:我创建了一个join-us-section.js ,其中有以下代码:
function addSection() {
function elCreating(elName, htmlEl, elClass, elID) {
elName = document.createElement(htmlEl);
elName.className = elClass;
elName.id = elID;
return elName;
}
function getElbyID(elName, searchedId) {
elName = document.getElementById(searchedId);
return elName;
}
function setAttributes(elem) {
for (var i = 1; i < arguments.length; i += 2) {
elem.setAttribute(arguments[i], arguments[i + 1]);
}
}
let sectionFour = elCreating("sectionFour", "section", "app-section app-section--image-program", "fourth-section");
let sectionParent = getElbyID("sectionParent", "third-section");
let parentSection = sectionParent.parentNode;
parentSection.insertBefore(sectionFour, sectionParent.nextSibling);
let heading2 = elCreating("heading2", "h2", "program-title");
let heading2Text = document.createTextNode("Join Our Program");
heading2.append(heading2Text);
let parent = getElbyID("parent", "fourth-section");
let heading3 = elCreating("heading3", "h3", "program-subtitle");
let heading3Text = document.createTextNode("Sed do eiusmod tempor incididunt");
heading3.appendChild(heading3Text);
let linebreak = elCreating("linebreak", "br");
heading3.appendChild(linebreak);
heading3Text = document.createTextNode("ut labore et dolore magna aliqua");
heading3.appendChild(heading3Text);
let submitFieldWrapper = elCreating("submitFieldWrapper", "form", "submitFieldWrapper");
parent.append(heading2, heading3, submitFieldWrapper);
let emailForm = elCreating("emailForm", "div", "form-wrapper");
let inputForm = elCreating("inputForm", "input", "form-input", "submit-info");
setAttributes(inputForm,
"type", "text",
"placeholder", "Email");
emailForm.appendChild(inputForm);
document.querySelector("form").addEventListener("submit", function (e) {
e.preventDefault();
console.log(inputForm.value);
});
let submitForm = elCreating("submitForm", "input", "app-section__button submit-btn");
setAttributes(submitForm,
"type", "submit",
"value", "Subscribe");
submitFieldWrapper.append(emailForm, submitForm);
}
export default addSection;
And main.js I have this: main.js我有这个:
import addSection from "./join-us-section.js";
window.onload = addSection;
var SectionCreatorFactory = function () {
this.createType = function (type) {
var sectionType;
if (type === "standard") {
addSection();
removeElement("fourth-section");
} else if (type === "advanced") {
let sectionFour = elCreating("sectionFour", "section", "app-section app-section--image-program", "fifth-section");
let sectionParent2 = getElbyID("sectionParent2", "third-section");
let parentSection = sectionParent.parentNode;
parentSection.insertBefore(sectionFour, sectionParent.nextSibling);
let heading2 = elCreating("heading2", "h2", "program-title");
let heading2Text = document.createTextNode("Join Our Advanced Program");
heading2.append(heading2Text);
let parent = getElbyID("parent", "fifth-section");
let heading3 = elCreating("heading3", "h3", "program-subtitle");
let heading3Text = document.createTextNode("Sed do eiusmod tempor incididunt");
heading3.appendChild(heading3Text);
let linebreak = elCreating("linebreak", "br");
heading3.appendChild(linebreak);
heading3Text = document.createTextNode("ut labore et dolore magna aliqua");
heading3.appendChild(heading3Text);
let submitFieldWrapper = elCreating("submitFieldWrapper", "form", "submitFieldWrapper");
parent.append(heading2, heading3, submitFieldWrapper);
let emailForm = elCreating("emailForm", "div", "form-wrapper");
let inputForm = elCreating("inputForm", "input", "form-input", "submit-info");
setAttributes(inputForm,
"type", "text",
"placeholder", "Email");
emailForm.appendChild(inputForm);
document.querySelector("form").addEventListener("submit", function (e) {
e.preventDefault();
console.log(inputForm.value);
});
let submitForm = elCreating("submitForm", "input", "app-section__button submit-btn");
setAttributes(submitForm,
"type", "submit",
"value", "Subscribe to Advanced Program");
submitFieldWrapper.append(emailForm, submitForm);
removeElement("fourth-section");
}
return sectionType;
}
}
function removeElement(elementId) {
var element = document.getElementById(elementId);
element.remove();
}
var factory = new SectionCreatorFactory();
factory.createType("standard");
You see,for advanced type i just copy pasted the code from join-us-section to create the new section.你看,对于高级类型,我只是从 join-us-section 复制粘贴代码来创建新部分。 But as i know,dublicating code is not recommended.
但据我所知,不建议复制代码。 First of all this copy paste doesn't work,it creates three sections with the same characteristics as from
function AddSection
.首先,这个复制粘贴不起作用,它创建了三个与
function AddSection
具有相同特征的部分。 I dont know how to make it smaller.我不知道如何使它变小。 I tried to do another function, to modify the element, to declare the variables global,but nothing works.
我试图做另一个 function,修改元素,声明全局变量,但没有任何效果。 Maybe i do the wrong aproach.
也许我做错了方法。 I searched the whole internet for any documentations, but i dont find anything that fits my task.
我在整个互联网上搜索了任何文件,但我没有找到任何适合我任务的东西。
I'll answer by asking a question to get you thinking.我会通过问一个问题来回答你,让你思考。 Could you use inheritance?
你能用 inheritance 吗? What about conditional rendering?
条件渲染呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.