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 が表示されませんが、trails[ をクリックしたときにのみ表示されます。私]。この問題を初心者にとってより複雑にする .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. 
     */
}

ヒントとして、配列にキーワーク 'new' を使用すると、 jsが不機嫌になります。代わりに '[]' を使用してください。その理由については、こちらをご覧ください:

于 2015-01-31T10:54:04.340 に答える