简体   繁体   English

Opera上的JS函数问题

[英]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. 在所有浏览器(包括IE)中,此功能都可以完美运行,但在Opera中则不能,这可能是错误的。

BTW, the error says this: 顺便说一句,错误说明:

The mistake is this: Uncaught exception: TypeError: Cannot convert 'document.getElementById(value)' to object 错误是这样的:未捕获异常:TypeError:无法将'document.getElementById(value)'转换为对象

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 . 使用提供的信息很难说,但是如果由于某种原因Opera无法找到您的元素之一,那么.get(0)将是undefined ,并且您将尝试访问undefinedtagName属性,这将导致一个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 . 这样可以确保在执行element.tagName之前存在一个元素。

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

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