2011-01-09 52 views
2

我有下面的代碼問題:Javascript和很長的字符串

function showTableData() 
{ 
    var tableArray; 
    var x = 0; 
    var theHTML; 

    for (i = 0; i < 7032; i++) 
    { 
     if (x = 0) 
     { 
      theHTML = '<tr>' + 
       '<th scope="row" class="spec">' + partNum[i] + '</th>' + 
       '<td>' + Msrp[i] + '</td>' + 
       '<td>' + blah[i] + '</td>' + 
       '<td>' + blahs[i] + '</td>' + 
      '</tr>' + theHTML; 
      x++; 
     }else{ 
      theHTML = '<tr>' + 
       '<th scope="row" class="specalt">' + partNum[i] + '</th>' + 
       '<td class="alt">' + Msrp[i] + '</td>' + 
       '<td class="alt">' + blah[i] + '</td>' + 
       '<td class="alt">' + blahs[i] + '</td>' + 
      '</tr>' + theHTML; 
      x--; 
     } 
    } 
    theHTML = '<table id="mytable" cellspacing="0">' + 
    '<tr>' + 
     '<th scope="col" abbr="Configurations" class="nobg">Part Number</th>' + 
     '<th scope="col" abbr="Dual 1.8">Msrp Price</th>' + 
     '<th scope="col" abbr="Dual 2">blahs Price</th>' + 
    '<th scope="col" abbr="Dual 2.5">Low Price</th>' + 
    '</tr>' + theHTML + '</table>'; 

    $('#example').append(theHTML); 
} 
</script> 

<div id="example"> 
</div> 

的問題之處在於$( '#例子')附加(theHTML);從不執行(或在頁面上顯示)。我認爲它是因爲這個字符串太長了!它在陣列中有超過7000個物品,所以我不知道這是否是其原因或其他東西?

任何幫助將是偉大的!謝謝!

大衛

+1

`如果(X = 0)`(9號線)應該是`如果(x == 0)`不知道這是否只是一個錯字雖然 – Hemlock 2011-01-09 03:52:09

+0

你確定函數調用正確嗎? – 2011-01-09 03:52:32

+2

一些建議。使用`push`構建數組而不是字符串連接。然後在添加html之前使用`array.join('')`。更好的性能。 – Hemlock 2011-01-09 03:55:13

回答

9

除了if (x = 0)實際上應當if (i % 2 === 0),你真的應該使用數組這裏提高性能。 join()方法而不是連接字符串。這與C#或Java中的StringBuilder具有類似的效果。

例如:

function showTableData() 
{ 
    var tableArray; 
    var theHTML = []; 
    theHTML.push('<table id="mytable" cellspacing="0">', 
    '<tr>', 
     '<th scope="col" abbr="Configurations" class="nobg">Part Number</th>', 
     '<th scope="col" abbr="Dual 1.8">Msrp Price</th>', 
     '<th scope="col" abbr="Dual 2">blahs Price</th>', 
    '<th scope="col" abbr="Dual 2.5">Low Price</th>', 
    '</tr>'); 

    for (var i = 0; i < 7032; i++) 
    { 
     if (i % 2 == 0) 
     { 
      theHTML.push('<tr>', 
       '<th scope="row" class="spec">', partNum[i], '</th>', 
       '<td>', Msrp[i], '</td>', 
       '<td>', blah[i], '</td>', 
       '<td>', blahs[i], '</td>', 
      '</tr>'); 
     } else { 
      theHTML.push('<tr>', 
       '<th scope="row" class="specalt">', partNum[i], '</th>', 
       '<td class="alt">', Msrp[i], '</td>', 
       '<td class="alt">', blah[i], '</td>', 
       '<td class="alt">', blahs[i], '</td>', 
      '</tr>'); 
     } 
    } 
    theHTML.push('</table>'); 

    $('#example').append(theHTML.join('')); 
} 
</script> 

<div id="example"> 
</div> 

之所以追加字符串'my' + ' appended' + ' string'比將字符串連接慢['my', ' joined', ' string'].join('')是因爲JavaScript strings are immutable因此在前者的例子有創建的每個2個字符串連時間的三分之一字符串,它是一個與向數組中添加新條目相比,操作非常昂貴。

另請參閱使用Array.push()和Array.join()的相同原理構建的Javascript StringBuilder project

該項目在IE中10000次連接的性能改進從1分鐘降低到0.23秒。

更新:爲了替換for-loop中的字符串連接,添加了對Array.join()的附加調用,以進一步提高客戶端渲染速度。 +添加了更好的鏈接到StringBuilder。

進一步更新:添加建議由鐵杉:

  1. 不再使用的使用Array.push的多個參數在一個時間在for循環
  2. 推幾個字符串限定var i = 0全局範圍的變量( )。
2

這條線:

if (x = 0) 

應該不會是

if (x == 0) 

可能是在這裏的錯誤導致腳本不能夠執行的其餘部分。

+0

謝謝,thephpdeveloper。修復了這個錯誤。 – StealthRT 2011-01-09 04:24:13

1

如果你已經在這裏使用jQuery,你可能想在這裏考慮jQuery templates,它很可能會清理你的代碼,讓你的生活更輕鬆。可能會有性能折衷(但我認爲字符串插值比JavaScript中的字符串串聯更快,所以這甚至可以更快?),但是您可能會或可能不會失去性能,這是一個更優雅的解決方案。您可以將整個模板放在一個地方(可能是外部文件,或者其中的一部分隱藏在文檔的DOM中,我沒有足夠的使用它來告訴您最佳做法是什麼),然後使用jQuery模板做字符串替換。有多個基於jQuery的模板框架,但我鏈接的是成爲jQuery的官方部分。

1

問題是.append jquery函數。長字符串不能使用.html或.append函數。相反,你需要使用.innerHTML

試試這個

document.getElementById('example').innerHTML=theHTML.join('');