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 '
        }
    });
    }

在这个例子中,我在另一个数组中调用一个数组元素,所以我不确定它是否正确,否则当点击 circle[i] 或 text[i] 时 .qtip 将不会显示,但只有在 onclick 轨迹时才会显示 [一世]。还有一个 .node 属性使这个问题对于初学者来说更加复杂。有任何想法如何改进代码以使其工作?

4

1 回答 1

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 在对数组使用键 'new' 时会变得暴躁,请改用 '[]',您可以在此处阅读原因:W3Schools.com - JS - Best Practices

于 2015-01-31T10:54:04.340 回答