为什么我得到window.getElementById不是一个函数

时间:2017-01-19 21:37:43

标签: javascript

为什么我得到的getElementyById不是函数?

window.getElementById('notes').addEventListener('keydown', function (e) {
    // Allow: backspace, delete, tab, escape, enter and .
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
         // Allow: Ctrl+A
        (e.keyCode == 188 && e.shiftKey === true) ||
         // Allow: Ctrl+A
        (e.keyCode == 190 && e.shiftKey === true) ||
        // Allow: Ctrl+A
        (e.keyCode == 191 && e.shiftKey === true) ||
        // Allow: Ctrl+A
        (e.keyCode == 65 && e.ctrlKey === true) ||
         // Allow: Ctrl+C
        (e.keyCode == 67 && e.ctrlKey === true) ||
         // Allow: Ctrl+X
        (e.keyCode == 88 && e.ctrlKey === true) ||
         // Allow: home, end, left, right
        (e.keyCode >= 35 && e.keyCode <= 39)) {
             // let it happen, don't do anything
             return;
    }
    // Ensure that it is a number or alphabet and stop the keypress
    if ((e.altKey) || e.keyCode == 191 || (e.keyCode < 48 || e.keyCode > 90) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
}, false);

1 个答案:

答案 0 :(得分:4)

使用document.getElementById("elementId")代替window.getElementById("elementId")