2012-12-11 95 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

相关问题