JSFiddle - React, Tailwind, and code Playground
by erika_sun
HTML
<input id='input'/>
<button onclick="add()">新增</button>
<div id="root">
</div>
JavaScript
var todos = [
{
title: "倒垃圾"
},
{
title: "繳電話費"
},
{
title: "採買本週食材"
},
];
function render()
{
const root = document.querySelector('#root')
const ul = document.createElement('ul')
root.textContent = ''
root.append(ul)
for( let todo of todos ){
const li = document.createElement('li')
const span = document.createElement('span')
span.textContent = todo.title
li.append(span)
ul.append(li)
}
}
function add(){
const input = document.querySelector('input')
todos.push({
title: input.value
})
input.value = ''
render();
}
render();