我有一個最令人沮喪的問題,我曾與編程語言。我讀了一些XML,然後嘗試顯示在網頁上。這樣做我沒有問題。 這裏是如何完成這個代碼。更新與循環對象與網頁的網頁
// File: readXML.js
var shared = [];
var sheet = new Array()
// Start function when DOM has completely loaded
$(document).ready(function(){
var bigo = new Object();
console.log("can you see me.");
var sheetJoint = new Object();
// get the sheet xml file
$.get("sheet1.xml",{},function(xml){
var attrs = [];
// this is a loop within a loop. we traverse the values in the xml to get end up with a key pair value of key: val
// in our case this works out to be A1 = 0 this is the first step to get the actual value from the sharedstring.xml
// Run the function for each row tag in the XML file
$(xml).find("row").each(function(i) {
//run the function for each c tag in the xml and get the attribute.
//this is the attribute that references the actual column.
$(this).find("c").each(function(i){
$('c',xml).each(function(i) {
v1 = $(this).attr("r");
bigo[v1] =v1;
bigo[v1]= $(this).find("v").text();
});
})});
//get the shared string elements to combine with the other
$.get("sharedStrings.xml",{},function(xml){
$('si',xml).each(function(i) {
shared.push($(this).find("t").text());
})});
});
combineObjects(bigo);//combine the the array and the object.
});
因爲我有兩個讀取兩個不同的XML文件,我不得不使用另一個函數來組合它們。這是這個功能。
function combineObjects(obj){
myHTMLOutput = '';
myHTMLOutput += '<table width="98%" border="1" cellpadding="0" cellspacing="0">';
myHTMLOutput += '<th>A</th>';
//mydiv=document.getElementById("ContentArea")
try{
var strt="";
var tempVal;
//loop throught the obejct and get the value from the returnTheValueSegment.
for (var ind in obj){
//if you want to print something to the log then just add this.
// pretty handy when trying to discover variable values. does not see to work well inside for loops thought.
// console.log("can you see me.");
tempVal = returnTheValueOfSegment(obj[ind]);
//bring the values
obj[ind] = tempVal;
}
for (var ind in obj){
mydata = BuildStudentHTML(ind);
myHTMLOutput = myHTMLOutput + mydata;
}
myHTMLOutput += '</table>';
$("#ContentArea").append(myHTMLOutput);
}
catch(err){alert(err)};
}
我在創建表時出現問題。它的基本命中或錯過... 如果我在Firefox中嘗試它,它只能工作,如果我使用螢火蟲,並通過代碼,否則它不會顯示錶格元素。
這裏是被調用來創建表的代碼。
function BuildStudentHTML(column1){
// Build HTML string and return
output = '';
output += '<tr>';
output += '<td>'+ column1 +'</td>';
output += '</tr>';
return output;
}
我可能做錯了什麼。我需要某種計時器嗎?是循環是快速和頁面不能刷新。如果有人能指出我正確的方向,我會永遠感激。
你知道'$ .get'是**異步**,對吧?並且在HTTP請求完成之前,返回的對象將不可用?您需要將返回的Deferred對象與'$ .when()'結合起來,然後在回調函數中完成「組合」工作。 – Pointy 2012-02-11 13:41:34
感謝您的答覆pointy.but你是否基本上說,即使在$ .get完成之前,該功能正在前進?您能否通過.get回撥指向正確的方向?我真的很新,並試圖瞭解jQuery。 – user677275 2012-02-11 13:52:04
是 - '$ .get()'立即返回**,即使它可能在HTTP請求完成之前有許多毫秒或幾秒。看@ wecsam的答案。 – Pointy 2012-02-11 14:03:25