2012-12-11 94 views
0

我不確定問題的標題是否正確。看代碼:通過數組和具有屬性的對象進行迭代

var trails = new Array(trail1, trail2, trail3, trail4, trail5, trail6, trail7, trail8, trail9, trail10, trail11, trail12, trail13); 
    var circles = new Array(circle1, circle2, circle3, circle4, circle5, circle6, circle7, circle8, circle9, circle10, circle11, circle12, circle13); 
    var texts = new Array(text1, text2, text3, text4, text5, text6, text7, text8, text9, text10, text11, text12, text13); 
    for(var i=0;i<=13;i++) { 
     $([trails[i].node,circles[i].node,texts[i].node]).qtip({ 
     content: { 
      text: 'test qtip', 
      title: {text: 'test', button: 'close'} 
     }, 
     position: { 
      target: 'mouse', 
      adjust: {mouse: false} 
     }, 
     show: { 
      event: 'click' 
     }, 
     style: 'qtip-rounded qtip-shadow qtip-blue', 
     hide: { 
      event: 'click ' 
     } 
    }); 
    } 

在這個例子中我真的調用另一個數組中的數組元素,所以我不知道這是正確的,但在其他方面.qtip不會顯示當點擊圓圈[I]或文字[我],但只有當點擊路徑[我]。還有一個.node屬性,使這個問題對於初學者來說更加複雜。有任何想法如何改善代碼,使其工作?

+0

究竟這些路徑究竟是什麼,cir文件和文本? –

+0

路徑,圈子和文本是由RaphaelJS生成的SVGs – user1793789

+0

jsfiddle會幫助... – Daniel

回答

0

第一:你的循環有「< =」在您的數組包含可能導致您遇到任何錯誤13個項目中,「< =」將重複14次......

只是清理一下代碼...(這部分是任意)

var trails = [],circles = [],texts = [], i = 13; 
while (i--){ 
    trails[i] = eval('trail'+i);//parses the text to return your js variable 
    circles[i] = eval('circle'+i); 
    texts[i] = eval('text'+i); 
    . . . 
    /** Continue with whatever else you wish to do inside the loop, 
    * I just included this bit to show how you can instantiate your 
    * arrays without having to hard code each of your variables... 
    * Also, it is possible to use the variables name as a reference 
    * inside the array like so: trails['trail'+i] = . . . 
    * that way you can still call each variable by name. 
    */ 
} 

而只是作爲小費,JS使用keywork「新」的陣列,使用時得到胡思亂想「[]」來代替,你可以看到爲什麼在這裏:W3Schools.com - JS - Best Practices

相關問題