如何从列表中删除添加的项目?

时间:2017-10-09 12:32:14

标签: javascript

我正在尝试学习JS,我决定制作一个真正的应用程序,以便在我做某些事情的同时学习。我试图制作一个TODO列表应用,我做了添加项目,但我无法弄清楚如何在点击它们时删除项目。

这里是代码自己:

             我的TODO' S

    <input id= "input" class="form-control" placeholder="Add items to your TODO list." type="text">

    <button id="button" class="button btn btn-primary btn-block">Add</button>

    <div class="items">
        <ul id= "listGroup" class="list-group">
        </ul>
    </div> <!-- JS Items !-->
</div> <!-- End Div -->

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="app.js"></script>

JS:

var button1 = document.getElementById("button")

button1.addEventListener("click", addItem)

function addItem() {

    let item = document.getElementById("input").value
    let add = document.getElementById("listGroup")

    let makeLi = document.createElement("li")
    let makeText = document.createTextNode(item)
    makeLi.className += "list-group-item"

    let icon = document.createElement("i")
    icon.className += "fa fa-times"

    add.appendChild(makeLi).appendChild(makeText)
}

function revomeItem() {
}

2 个答案:

答案 0 :(得分:0)

makeLi.onclick = function(){
 this.remove();
};

答案 1 :(得分:0)

var div1 = document.getElementById("div-01")
    div1.addEventListener("click", removeItem);
    function removeItem(e){
        e.target.remove();
    }

这应该有用..