[英]How to add an instantiated objects to the DOM? Simulating React in Vanilla JS
我正在嘗試為我的班級完成一個可選項目,過去一個月左右的時間里我們一直在使用React,而我對框架已經很滿意了。 但是,為了更好地了解事物在幕后的實際運行方式,我嘗試重新創建實例化對象的過程,並使用原始JavaScript在頁面上呈現它們。
我現在發帖是因為我碰壁了,我很難在網上找到有用的材料。
在下面的代碼中,我成功地查詢了我的輸入和一個按鈕,我想將一個對象渲染到顯示Idea
類唯一實例的頁面上。 該對象帶有一些按鈕:“收藏夾”,“刪除”,“贊成”和“反對”。
到目前為止,我已經完成了什么:
我正在嘗試做的是:
ideas
數組中的每個對象元素呈現到DOM 到目前為止,我目前編寫的邏輯是:
//buttons
const saveButton = document.querySelector(".save-button");
//inputs
const titleInput = document.querySelector("[name='title']");
const bodyInput = document.querySelector("[name='body']");
//output
const outputSection = document.querySelector(".main-outputs");
let titleValue = "";
let bodyValue = "";
const ideas = [];
titleInput.addEventListener("keyup", e => {
titleValue = "";
titleValue += e.target.value;
});
bodyInput.addEventListener("keyup", e => {
bodyValue = "";
bodyValue += e.target.value;
});
saveButton.addEventListener("click", e => {
e.preventDefault();
const ideaObject = new Idea(titleValue, bodyValue);
ideas.push(ideaObject);
console.log("ideas: ", ideas);
render(ideaObject, outputSection);
});
const render = function(template, node) {
if (!node) return;
node.innerHTML += template;
};
class Idea {
constructor(title, body) {
this.title = title;
this.body = body;
this.favorite = false;
this.quality = "swill";
this.id = Date.now();
this.content = this.content;
}
renderIdea() {}
}
每個元素“應該”在頁面上呈現的html。
<article class="main-output-card">
<header>
<img src="./icons/star.svg" alt="" />
<img src="./icons/delete.svg" alt="" />
</header>
<section>
<h3 class="idea-title">Idea Title</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque
consectetur voluptas fuga accusantium!
</p>
</section>
<footer>
<img src="./icons/upvote.svg" alt="" />
<h5 class="idea-quality">Quality: <span>Swill</span></h5>
<img src="./icons/downvote.svg" alt="" />
</footer>
</article>
任何幫助將不勝感激,這似乎是一個很大的挑戰,但我感到自己不知所措。
很想知道我該怎么做/我在做什么錯。
謝謝!
因此,如果您想從頭開始渲染整個內容(我想是的),那么您可以這樣做。 都是純JavaScript,沒有JQuery或類似的東西。
有一件非常重要的事情要記住。 就像React,Vue等一樣,如果用戶在瀏覽器中禁用了JavaScript,則在打開頁面時他們將看不到任何內容。
/* your other existing functions */
const createImg = function(src, alt) {
let img = document.createElement('img');
img.setAttribute('src', src);
img.setAttribute('alt', alt);
return img;
}
const createTextElement = function (type, text, className) {
let el = document.createElement(type);
el.innerText = text;
if (className) el.className = className;
}
class Idea {
constructor(title, body) {
this.title = title;
this.body = body;
this.favorite = false;
this.quality = "swill";
this.id = Date.now();
this.content = this.content;
}
renderIdea() {
// Create the main container
let article = document.createElement('article');
article.className = 'main-output-card';
// Create the header container and add the icons to it
let articleHeader = document.createElement('header');
articleHeader.appendChild(createImg('./icons/star.svg', ''));
articleHeader.appendChild(createImg('./icons/delete.svg', ''));
// Create the content section and add the content to it
let articleSection = document.createElement('section');
articleSection.appendChild(createTextElement('h3', this.title, 'idea-title'));
articleSection.appendChild(createTextElement('p', this.body));
// Create the article footer and add content to it
articleFooter = document.createElement('footer');
let articleFooterQuality = document.createElement('h5');
articleFooterQuality.className = 'idea-quality';
articleFooterQuality.innerHtml = `Quality: <span>${this.quality}</span>`;
articleFooter.appendChild(createImg('./icons/upvote.svg', ''));
articleFooter.appendChild(articleFooterQuality);
articleFooter.appendChild(createImg('./icons/downvote.svg', ''));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.