1

所以只是调试别人的代码,我已经修复了 Firefox 中的所有内容,现在又修复了 Chrome,但是当我尝试选择一个选项来选择所有子节点并打开它们的相关图表时,我收到了一个奇怪的错误:

未捕获的类型错误:无法调用未定义的方法“substr”

我知道问题是(显然)这一行:

var graph={id:seqId,entityName:flags[6].substr(1).substr(0,flags[6].length-2),entity:flags[5].substr(1).substr(0,flags[5].length-2),idCounter:flags[4],counterName:flags[3].substr(1).substr(0,flags[3].length-2),ivmanager:flags[7].substr(1).substr(0,flags[7].length-2),chart:null,pointsToShowX:null,borneInf:null,unite:"",idInstance:flagsInstance[2].substr(1).substr(0,flagsInstance[2].length-2),instanceName:flagsInstance[3].substr(1).substr(0,flagsInstance[3].length-2),listPdsNull:new Array(), countInstance:idTreeview+"_"+i, countGraph:-1};

但我不知道为什么这会导致错误,因为一切对我来说都是正确的?!substr 在函数之前和之后被无数次使用,所以我不知道为什么 Chrome 会向我抛出这个错误。

完整的功能如下。我相信问题出在第二部分。

function loadGraphs(idTreeview,idSelectGraph)
{

    var count=0;
    var fn=$('#'+idSelectGraph)[0].attributes[1].value;
    fn=fn.substr(12).substr(0,fn.length-2);
    var flags=fn.split(new RegExp(","));
    var data=''; 

    var idInstanceTreeview = idTreeview;

if($('#counterTreeviewUL'+idInstanceTreeview).length == 0)      
{

    var fnInstance=$('#'+idSelectGraph)[0].attributes[1].value;

    var flagsInstance=fnInstance.split(new RegExp(","));

    var graph = {
            id: seqId,
            entityName: flags[6].substr(1).substr(0, flags[6].length - 2),
            entity: flags[5].substr(1).substr(0, flags[5].length - 2),
            idCounter: flags[4],
            counterName: flags[3].substr(1).substr(0, flags[3].length - 2),
            ivmanager: flags[7].substr(1).substr(0, flags[7].length - 2),
            chart: null,
            pointsToShowX: null,
            borneInf: null,
            unite: "",
            idInstance: flagsInstance[2].substr(1).substr(0, flagsInstance[2].length - 2),
            instanceName: flagsInstance[3].substr(1).substr(0, flagsInstance[3].length - 2),
            listPdsNull: new Array(),
            countInstance: idTreeview + "_" + i,
            countGraph: -1
        };

    seqId++;
    graphs[graphsLastId]=graph;
    graphsLastId++;

    }

else 
  {
    for(var i=0;i<$('#counterTreeviewUL'+idInstanceTreeview)[0].children.length;i++)
        ($('#counterTreeviewUL'+idInstanceTreeview)[0].children[i].children[0].checked)?++count:count;

    if ($('#counterTreeviewUL'+idInstanceTreeview)[0].children.length == count)
    {
        $('#'+idSelectGraph)[0].checked=true;
        $('#'+idSelectGraph)[0].indeterminate=false;
    }
    else if (count==0)
    {
        $('#'+idSelectGraph)[0].checked=true;       
        $('#'+idSelectGraph)[0].indeterminate=false;        
    }
    else
    {
        $('#'+idSelectGraph)[0].checked=true;
        $('#'+idSelectGraph)[0].indeterminate=false;
    }   
        for (var i=0;i<$('#counterTreeviewUL'+idTreeview)[0].children.length;i++)
    {   
        if (!$('#counterTreeviewUL'+idTreeview)[0].children[i].children[0].checked)
        {   
            $('#counterTreeviewUL'+idTreeview)[0].children[i].children[0].checked=true;


       var fnInstance=$('#counterTreeviewUL'+idTreeview)[0].children[i].children[0].attributes[1].value;

       fnInstance=fnInstance.substr(15).substr(0,fnInstance.length-2);  
       var flagsInstance=fnInstance.split(new RegExp(","));


            var graph = {
                    id: seqId,
                    entityName: flags[6].substr(1).substr(0, flags[6].length - 2),
                    entity: flags[5].substr(1).substr(0, flags[5].length - 2),
                    idCounter: flags[4],
                    counterName: flags[3].substr(1).substr(0, flags[3].length - 2),
                    ivmanager: flags[7].substr(1).substr(0, flags[7].length - 2),
                    chart: null,
                    pointsToShowX: null,
                    borneInf: null,
                    unite: "",
                    idInstance: flagsInstance[2].substr(1).substr(0, flagsInstance[2].length - 2),
                    instanceName: flagsInstance[3].substr(1).substr(0, flagsInstance[3].length - 2),
                    listPdsNull: new Array(),
                    countInstance: idTreeview + "_" + i,
                    countGraph: -1
                };

            seqId++;            
            graphs[graphsLastId]=graph;             
            graphsLastId++;
        }
    }

 }
    graphsToLoad=false;
    updateAllGraphs();
    loading=false;

    if($('#counterTreeviewUL'+idInstanceTreeview).length == 0)
    {

      if($('#'+idSelectGraph)[0].checked)
        {
        countSelectedGraphs(graphsLastId, flags[5].substr(1).substr(0,flags[5].length-2), 0);
        }
      else 
        {
        countSelectedGraphs(graphsLastId, flags[5].substr(1).substr(0,flags[5].length-2), 0);
        }
    }

    else 
    {
    countSelectedGraphs(graphsLastId, flags[5].substr(1).substr(0,flags[5].length-2), 0);       
    }
}

任何有关导致此看似随机错误的原因的见解都将不胜感激,因为我在网上找不到太多关于此错误的信息(我看到它发生的情况与我的情况完全不同),谢谢!

4

2 回答 2

2

线

fn=fn.substr(12).substr(0,fn.length-2);

从创建一个子字符串开始,有效地从字符串中剥离前 12 个字母,并留下“5”。(在这种情况下,第二个substr实际上什么都不做)

因为 fn 在您分配时flagsflags“5”,所以也是“5”,这是一个 1 字符的字符串,因此flags[6] == undefined(或任何大于 0 的索引)。只需在控制台中尝试以下 3 行:

var x = "5"; 
x[0]; // returns "5"
x[1]; // returns undefined
于 2012-10-26T14:29:15.830 回答
0

这是您的可读代码:

var graph = {
    id: seqId,
    entityName: flags[6].substr(1).substr(0, flags[6].length - 2),
    entity: flags[5].substr(1).substr(0, flags[5].length - 2),
    idCounter: flags[4],
    counterName: flags[3].substr(1).substr(0, flags[3].length - 2),
    ivmanager: flags[7].substr(1).substr(0, flags[7].length - 2),
    chart: null,
    pointsToShowX: null,
    borneInf: null,
    unite: "",
    idInstance: flagsInstance[2].substr(1).substr(0, flagsInstance[2].length - 2),
    instanceName: flagsInstance[3].substr(1).substr(0, flagsInstance[3].length - 2),
    listPdsNull: new Array(),
    countInstance: idTreeview + "_" + i,
    countGraph: -1
};

需要确保每次调用时substr,您调用它的“字符串”都存在。

于 2012-10-26T13:54:29.300 回答