简体   繁体   中英

Cannot call method … of undefined

So I have made this code, it works almost perfectly. The one thing is that setupListener method returns a error at the end, probably it finds a property I don't want to find. I've been struggling with this for a while, but I clearly don't have that much experience with js to solve it. I have setup some console.log methods for debugging. You use this function like this: setupListener(Id or Class or a Tag name as string, event to watch on as string, and an action) ;

eg. setupListener('.pun', 'click', function (e){ console.log(e); });

    var getElement = function (onStr) {
    if (onStr.indexOf('.') === 0) {
        onStr = onStr.slice(1);
        return document.getElementsByClassName(onStr);
    } 

    if (onStr.indexOf('#') === 0) {
        onStr = onStr.slice(1);
        return document.getElementById(onStr);
    } 

    if (onStr.indexOf('#') !== 0 && onStr.indexOf('.') !== 0) {
        return document.onStr = document.getElementsByTagName(onStr);
    }
};

var setupListener = function (elementStr, eventStr, action) {
    var tempElement = getElement(elementStr);

        // element a collection and has addEventListener method
        if (tempElement.length > 1 && tempElement[1].addEventListener) {
            for (var i = 1; tempElement.length >= i; i++) {
                if (typeof(tempElement[i].addEventListener) !== "undefined")
                    console.log('1'); //debugging
                    tempElement[i].addEventListener(eventStr, action);
            }   
        }

        // IE < 9 Support
        // element a collection and has NOT addEventListener method
        else if (tempElement.length  > 1 && !tempElement[1].addEventListener) {
            for (var i = 1; tempElement.length >= i; i++) {
                if (typeof(tempElement[i].addEventListener) !== "undefined")
                    console.log('2'); // debugging
                    tempElement[i].attachEvent(eventStr, action);
            }
        }

        // element not a collection and HAS addEventListener method
        else if (!tempElement.length > 1 && tempElement.addEventListener) {
            console.log('3'); // debugging
            tempElement.addEventListener(eventStr, action);
        }

        // element not a collection and has NOT addEventListener method
        // IE < 9 support
        else if (!tempElement.length > 1 && !tempElement.addEventListener) {
            console.log('4'); // debugging
            tempElement.attachEvent(eventStr, action);
        }

    else { 
     console.log('5'); // debugging
    }
};

Your problem is in the part of the collection of elements. Note that you get the first element of the array by array[0] so you get the last element by array[array.length-1] . But what you are doing when you are iterating over the arrays is, starting at 1 and iterating to array.length . When you try to access an unavailable array-element (as array[array.length] ) you will get undefined , and thats ecaxtly what your error says. So you have to change the bounds in the for -loops. Ie

for (var i = 0; i < tempElement.length; i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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