2011-11-10 62 views
0
if (_this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"][imageNo] != undefined) { 
    if (imageNo > (_this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"].length - 1)) { 
     imageNo = 0; 
     _this.categoriesCurrentImage[categoryIndex] = imageNo; 
    } 
    var img = _this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"][imageNo]; 

    _this.showImage(img, divId); 
    _this.categoriesCurrentImage[categoryIndex]++; 
    _this.categoryIndex++; 
} else { 

    if (imageNo > (_this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"].length - 1)) { 
     imageNo = 0; 
     _this.categoriesCurrentImage[categoryIndex] = imageNo; 
    } 

    var img = _this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"]; 
    _this.showImage(img, divId); 
    _this.categoriesCurrentImage[categoryIndex]++; 
    _this.categoryIndex++; 
} 

谁能告诉我为什么。如果语句是真的,代码去其他地方?

我最终在else子句中,即使if为真? 这对我来说绝对没有意义。

如果在suz:image节点下面有超过1个图像,它应该落入else中。它有99%的时间。

+2

你的代码格式不。解决这个问题,这个错误应该变得更加明显。 – zzzzBov

+2

使用一些更多变量来使代码更易于阅读和调试。例如,把var img = _this.tiles [“suz:SuzukiHubResponse”] [“suz:categories”] [category] ​​[aspect] [“suz:image”];更高,并在适当的测试中使用它。 –

回答

0

你需要在引号包裹undefined

!= "undefined" 
+3

将未定义的引号放在引号中并不是必需的方式。当它是'typeof'的结果时,它只需要在引号中。否则,它不包含引号。有关更多信息,请参阅https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined。 – jfriend00

+0

有人可以向我解释为什么把这一切都放在一个变量中做的伎俩?我把大部分的json结构放入一个名为img的变量中。现在它可以工作。 – Puzzle84