简体   繁体   中英

Problem with JS function on Opera

function getLast(tagNm) {
    /* This function search the last element
     * that use this tagName */
    var cont = 0;

    $.each(window.wdgList, function (index, value) {
        if (value != undefined) {
            if ($("#" + value).get(0).tagName == tagNm) {
                cont += 1;
            }
        }    
    });

    return cont;
}

And with all the browser-including IE- this function works perfect but with Opera doesn't, what could be wrong.

BTW, the error says this:

The mistake is this: Uncaught exception: TypeError: Cannot convert 'document.getElementById(value)' to object

Hard to say with the info provided, but if for some reason Opera isn't finding one of your elements, then .get(0) will be undefined , and you'll be attempting to access the tagName property on undefined which will result in a TypeError .

You should perhaps check that an element was found first.

$.each(window.wdgList, function (index, value) {
    if (value != undefined) {
        var el = $("#" + value).get(0);
        if ( el && el.tagName == tagNm ) {
            cont += 1;
        }
    }    
});

This makes sure there's an element before doing element.tagName .

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