Null in not object(评估itemText.style)

时间:2016-03-07 06:14:31

标签: javascript css object null

在待办事项列表中,我试图查看已检查的项目,但我收到错误 - " Null in not object(评估' itemText.style&#39 ;)"

任何人都可以解释我应该如何改变这一点来进行罢工工作吗? 如果可能的话,我也试图避免将CSS放入我的HTML文件中。

function removeItem() {
    var boxId = this.id.replace("boxId_", "");
    var itemText = document.getElementById("item_", + boxId);
    itemText.style.setProperty("text-decoration", "line-through"); //error here
}

function addNewItem(list, itemText) {
    totalItems++;

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

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

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

var btnNew = document.getElementById("btnAdd");

var totalItems = 0;
var inItemText = document.getElementById("inItemText");
inItemText.focus(); 

btnNew.onclick = function() {
    var itemText = inItemText.value;

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

    addNewItem(document.getElementById("todoList"), itemText);
};

1 个答案:

答案 0 :(得分:2)

正是因为这条线

boxId

收尾报价后有一个逗号。您的代码基本上忽略了getElementById,因为它被视为var itemText = document.getElementById("item_" + boxId); 忽略的另一个参数。

制作

    function humanize() {
        return Math.round(Math.random() * (200 - 30)) + 30;
    }

    //Delete final character in modified string
    function deleteCharacter(text) {
        //return everything but the last character
        text = text.substring(0, text.length - 1);
        return text;
    }

    //Insert character_added at end of text
    function addCharacter(text, character_added) {
        text = text + character_added;
        return text;
    }

    //typos[char].error is just a self reference, it is not used
    var typos = {
    }

    var timeOut;
    var txtLen = txt.length;
    var char = 0;
    $('textarea').attr('placeholder', '|');
    function typeIt() {
        modified_txt += txt.charAt(char);
        $('textarea').attr('placeholder', modified_txt + '|');

        if (char === txtLen) {
            $('textarea').attr('placeholder', $('textarea').attr('placeholder').slice(0, -1)); // remove the '|'
            return; //Stop the loop once text is completely written.
        }

        var test = typos[char];
        if (test !== undefined) {
            setTimeout(function () {
                var chunk_one = test.correction(modified_txt);
                modified_txt = chunk_one;
                char++;
                typeIt();
            }, humanize());
        }
        //If no typos are found then move to the next character
        else {
            setTimeout(function () {
                char++;
                typeIt();
            }, humanize());
        }
    }

    $(function () {
        typeIt();

    });//end jquery