为什么document.getElementByID会抛出错误?

时间:2017-08-07 11:26:17

标签: javascript html

目前正在尝试编写一个脚本,允许网站将用户电子邮件添加到邮件列表中以获取新闻稿(在早期阶段)

每次我运行代码时都会收到以下错误:

Newsletter.js:2 Uncaught TypeError: document.getElementByID is not a function
    at Newsletter.js:2
    at Newsletter.js:11

我知道这在第2行和第11行遇到了问题,我只是看不出是什么问题。

我已经包含了所有相关的HTML和CSS。

非常感谢任何帮助。

表单位于页脚区域,所以我刚刚包含HTML的页脚:

<footer class="container-fluid text-center">
    <p id="TestSubmit">Online Store Copyright</p>  
    <form class="form-inline" id = "NewsletterEmail">Get deals:
            <input type="email" class="form-control" size="50" 
            placeholder="Email Address" id="EmailAddress">
            <button type="button" class="btn btn-danger" id="Submit">Sign 
    Up</button>
    </form>

    <script src="JS/Utilities.js"></script>
    <script src="JS/Newsletter.js"></script>
</footer>

function addEvent(el, event, callback) {
    if ('addEventListener' in el) {                     
        el.addEventListener(event, callback, false);    
    } else {                                            
        el['e' + event + callback] = callback;          
        el[event + callback] = function() {             
            el['e' + event + callback](window.event);   
        };
        el.attachEvent('on' + event, el [event + callback]);
    }
}

(function(){
    var form = document.getElementByID('NewsletterEmail'); 

    addEvent(form, 'Submit', function(e){
        e.preventDefault();
        var elements = this.elements;
        var email = elements.email.value;
        var msg = 'Welcome ' + username;
        document.getElementByID('TestSubmit').textContent = msg;
    });
}());

2 个答案:

答案 0 :(得分:1)

var form = document.getElementById('NewsletterEmail'); 

这是正确的,不是ID而是Id。

答案 1 :(得分:1)

getElementById而非getElementByID

var form = document.getElementById('NewsletterEmail');