这是代码...

https://jsfiddle.net/6n2k65zs/


尝试添加新项目,您会因为某些原因看到它不起作用,但应该...
我在代码中找不到任何错误,请有人帮我解决吗?

有人知道任何好的调试器吗?调试JS是一场噩梦!

谢谢。



<!DOCTYPE html>
<html>
<head>
    <title>JavaScript To-Do List</title>
    <link href="css/style.css" rel="stylesheet">
</head>

<body>
        <input id="input" type="text">
        <button id="btn">Add</button>
        <hr>
        <ul id="todo">

        </ul>

        <ul id="done">

        </ul>

<!--        javascript anonymous self-invoking function         -->
<!--        Function expressions will execute automatically     -->
<script>
    // from outside the action you wont be able to access the variables
    // prevents another variable with a same name from conflicting
    (function(){
        var input = document.getElementById('input');
        var btn = document.getElementById('btn');
//          Object for the lists
// the reason im using ID is because ID can only be named once rather than a class which can be named 100's of times
        var lists = {
            todo:document.getElementById('todo'),
            done:document.getElementById('done')
        };

        /*   Parameter is string
         create a list element which is stored in 'el' and returns it
         */
        var makeTaskHtml = function(str, onCheck) {
            var el = document.createElement('li');
            var checkbox = document.createElement('input');
            var label = document.createElement('span');

            label.textContent = str;
            checkbox.type = 'checkbox';
            checkbox.addEventListener('click', onCheck);
//            el.textContent = str;
//            can use this method to move an element from one element to another
            el.appendChild(checkbox);
            el.appendChild(label);
            // Text content is grabbing the text from the text box and storing it in variable el.
            return el;
        };
        var addTask = function(task) {
        	lists.todo.appendChild(task);
        };

        var onCheck = function(event){
            var task = event.target.parentElement; //targets the item clicked
            var list = task.parentElement.id;

            //lists.done.appendChild(task);
            //swaps the 2 objects around
            lists[list === 'done' ? 'todo' : 'done'].appendChild(task);
            this.checked = false;

            input.focus();

        };

        var onInput = function() {
            var str = input.value.trim; // trim removes white space...

            if (str.length > 0) {
                addTask(makeTaskHtml(str, onCheck));
                input.value = '';
                input.focus();
            }
        };

        btn.addEventListener('click', onInput);
        input.addEventListener('keyup', function(event){
            var code = event.keyCode;
            console.log(code);

            if (code === 13) {
            	onInput();
            }
        });
        input.focus();

        addTask(lists.todo, makeTaskHtml('Test done', onCheck));

    }());

</script>


</body>
</html>

最佳答案

在我看来,您不是将trim称为方法,而是将其作为变量访问?

尝试在修饰中添加():

var onInput = function() {
    var str = input.value.trim(); // trim removes white space...

关于javascript - 有人可以验证此JavaScript代码(待办事项列表),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33045501/

10-12 12:51