我有一个带jquery mobile的多页设计。在第一个站点上,我使用onklick()调用一个函数来用值填充全局数组。现在我想在第二个站点上显示这些值,但我无法使用document.write(array)显示它们
答案 0 :(得分:0)
要将数据附加到DOM,首先需要选择要附加的元素,然后在您的情况下迭代数组,将每个索引的值添加到DOM:
//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');
}
});
document.write
的文档:https://developer.mozilla.org/en/document.write
这是一个JSPerf,用于显示某些循环之间的性能差异:http://jsperf.com/jquery-each-vs-for-loops/2