1

リンクをクリックすると、(定義済みのリストから) ランダムなホームページが表示されるようにします。本当にこれは 2 つの質問なので、質問 1 は可能ですか? はいの場合、どのように?

ファイルが別のフォルダにある場合はどうなりますか? コードは次のようになりますか?

var arrPages = [   'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/feyra/feyra.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/rayne/rayne.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/adrienna/adrienna.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/averya/aveyra.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/demaen/demaen.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/phoenyx/phoenyx.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/raven/raven.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/trysten/trysten.html', 'http://neo.graceland.edu/~rmthomas/gothyc%20cafe/xiandre/xiandre.html'];
    document.getElementById('13').onclick = function () {
        var randUrl = arrPages[Math.floor(Math.random() * arrPages.length)];
        // Redirect/popup/new tab
        window.open(randUrl,'test');
    }
4

1 に答える 1

2
// A predefined list of pages
        var arrPages =
            [
                'http://google.com',
                'http://yahoo.com',
                'http://xtra.co.nz',
                'http://asdf.com'
            ];

        // Get the element
        var aLink = document.getElementById('aLink');

        // Bind the click event of aLink
        aLink.onclick = function () {

            // Get an integer that'll determine the index of arrPages to access
            var randomInt = Math.floor(Math.random() * arrPages.length);

            // Get the element with an index of randomInt
            var randUrl = arrPages[randomInt];

            // Here you can do whatever you like with randUrl, in this case 
            //  we'll open a new window that'll point to randUrl
            //   e.g. http://xtra.co.nz
            window.open(randUrl, 'test');
        }

設定に使用される関数の説明については、Explain Math.floor(Math.random())randomInt を参照してください。

于 2012-11-26T00:19:31.357 に答える