javascript - 当我在跨度 Javascript 上单击它们时,元素不会消失

标签 javascript html css

好吧,终于快完成本教程了,我在最后一部分,我可以单击跨度上的元素使它们消失,我单击它们并且它们没有消失有人知道为什么吗?这是我的代码

 <!doctype html>
<html>
    <head>

    <title>To do list with html and javascript</title>
    <style>
     ul { list-style: none; padding: 0; margin: 0; width: 400px;}
     li { border: 1px solid #ccc; background: #eee; padding: 10px 15px; color: #000; }
     li span { padding-left: 10px;}
     .checked { text-decoration: line-through; font-weight: bold; color: #c00;}
        </style>
    </head>
    <body>
<h1>To Do List</h1>
<p><input type="text" id="inItemText"/></p> 

<ul id="todolist">
</ul>



<script src="todo.js"></script>
    </body>
</html>

function updateItemStatus() {
    var cbId = this.id.replace("cb_","");
    var itemText = document.getElementById("item_" + cbId);

    if (this.checked) {
        itemText.className = "checked";
    } else {
        itemText.className = "";
    }
}

function removeItem() {
    var spanId = this.id.replace("item_" + "");
    document.getElementById("li_" + spanId).style.display = "none";

}



function addNewItem(list, itemText){

    var date = new Date();
    var id = "" + date.getHours() + date.getMinutes() + date.getSeconds() + date.getMilliseconds(); 

    var listItem = document.createElement

("li");
        listItem.id = "li_" + id;
        var checkBox = document.createElement ("input");
        checkBox.type = "checkbox";
        checkBox.id = "cb_" + id;
        checkBox.onclick = updateItemStatus;


        var span = document.createElement("span");
        span.id = "item_" + id;
        span.innerText = itemText;
        span.onclick = removeItem;


        listItem.appendChild(checkBox);
        listItem.appendChild(span);

        list.appendChild(listItem);

    }


    var inItemText = document.getElementById("inItemText");
    inItemText.focus();
    inItemText.onkeyup = function(event) {


        if (event.which == 13) {
            var itemText = inItemText.value;

            if (!itemText || itemText == "undefined") {
                return false;
            }

            addNewItem(document.getElementById("todolist"), itemText);

            inItemText.focus();
            inItemText.select();
        };

}

最佳答案

您的代码中似乎有拼写错误。 replace 方法接受两个由 逗号 分隔的参数。在您的代码中有一个 + 符号而不是 ,

使用下面的代码。 Working Sample

function removeItem() {
    var spanId = this.id.replace("item_", ""); //Note the change
    console.log(this.id + spanId);
    document.getElementById("li_" + spanId).style.display = "none";

}

关于javascript - 当我在跨度 Javascript 上单击它们时,元素不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284725/

相关文章:

javascript - 如何只允许滚动条滚动

html - 两列布局中的粘性页脚位置

JavaScript:确定变量是否为对象文字

javascript - d3 美国 map 缩放加载至标记状态

iphone - 在 iphone SDK 中删除特定的 HTML 标签

php - 从不同目录深度的不同页面动态添加相同的CSS文件

php - 如何在HTML5中实现简单的音频播放列表

Javascript 克隆和循环问题

javascript - 已更改类的背景颜色属性不起作用

javascript - 带有 Typescript 和 react-redux 的有状态组件 : Argument of type 'typeof MyClass' is not assignable to parameter of type Component