简体   繁体   English

“对象不支持......”IE8在声明普通变量时停止

[英]“Object doesn't support…” IE8 stops at declaring ordinary variable

I'm trying to make a form send its data through AJAX and cancel the event sans jQuery, just for learning native javascript, which can never be bad, i figured. 我试图让表单通过AJAX发送它的数据并取消事件sans jQuery,只是为了学习原生的javascript,这永远不会坏,我想。 Anyway, this code is returning the error "Object doesn't support this property or method" in IE8 at the line where I declare variables s and r in the send() function. 无论如何,这段代码在IE8中返回错误“Object不支持这个属性或方法”,我在send()函数中声明了变量s和r。 I figured the problem must actually be elsewhere? 我认为问题实际上必须在其他地方吗? Code works in both Firefox and Chrome, returning no errors. 代码适用于Firefox和Chrome,不会返回任何错误。 Ideas? 想法?

// Function to serialize form
function serialize() {
    var a = document.getElementsByTagName('input'), b = '';
    for (i = 0; i < a.length; i++) {
        b += a[i].name + '=' + a[i].value + '&';
    }
    return b.substring(0, b.length - 1);
}

// Function to execute when user submits form
function send(evt) {
    // Prevent the page from reloading
    if (evt.preventDefault) {
        evt.preventDefault();
    } else {
        evt.returnValue = false;
    }
    // Declare DOM variables for quick access
    var s = document.getElementsByClassName('skicka')[0], r = document.getElementById('return');
    // Hides the submit button and return text
    s.style.visibility = 'hidden';
    r.style.visibility = 'hidden';
    // Initialize and send data and request to login.php
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'login.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send(serialize());
    // Check for return value from login.php
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.responseText == true) {
                // If response if true, reload page
                window.location.reload(true);
            } else {
                // If response is false, reset form and show response
                s.style.visibility = 'visible';
                r.style.visibility = 'visible';
                r.innerHTML = xhr.responseText;
            }
        }
    };
    return false;
}

// Declare event listeners
if (window.addEventListener) {
    window.addEventListener('load', function() {
        document.forms[0].addEventListener('submit', send, false);
    }, false);
} else {
    window.attachEvent('onload', function() {
        document.forms[0].attachEvent('onsubmit', function() {
            send(window.event);
        });
    });
}

IE8 does not support .getElementsByClassName() . IE8不支持.getElementsByClassName() See the Ultimate GetElementsByClassName for a pure JavaScript implementation that will work in IE. 有关可在IE中使用的纯JavaScript实现,请参阅Ultimate GetElementsByClassName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM