2012-04-12 10 views
0

jquery mobileを使用したマルチページデザインがあります。最初のサイトでは、onklick()で関数を呼び出し、グローバル配列に値を入力します。今、私は2番目のサイトにこの値を表示したいですが、私はdocument.write(配列)でそれらを表示することはできませんjquery mobile multipageを使用したグローバル変数

+0

wheあなたは "サイト"と言っていますが、あなたはページ(data-role = "page")を参照していますか? – JonWells

+0

はい。私は、ページ(data-role = "page")がロードされたときに何とかjavascriptを起動しなければならないと思います。 javascriptファイルがホールhtmlファイルにリンクされているため、グローバル配列が表示されるはずです。 – AdrianoCelentano

答えて

0

データを追加するには、最初に追加する要素を選択する必要があります。その後、あなたのケースでDOMに各インデックスの値を加算し、配列を反復:document.writeため

//here is the array of values 
var myArr = [...]; 

//wait for out page to initialize 
$(document).delegate('#my-page-id', 'pageinit', function() { 

    //create an array to buffer the output 
    var output = []; 

    //use a fast loop to add the value at each index to an array, 
    //in this case I'm adding some HTMl markup to it as well 
    for (var i = 0, len = myArr.length; i < len; i++) { 
     output.push('<li>' + myArr[i] + '</li>'); 
    } 

    //check to see if there were any indexes in the array 
    if (output.length) { 

     //append a list-view widget with the info from myArr to the content section of the current page 
     $(this).children('.ui-content').append('<ul data-role="listview">' + output.join('') + '</ul>').trigger('create'); 
    } 
}); 

ドキュメント:https://developer.mozilla.org/en/document.write

ここではいくつかのループ間の性能差を示すことJSPerfです:http://jsperf.com/jquery-each-vs-for-loops/2

+0

あなたが私の質問を理解してくれてうれしいです:-) – AdrianoCelentano

関連する問題