2011-03-24 47 views
1

注意:最初有這被列爲內存泄漏。深入研究之後,我發現這不是一個記憶問題。這只是一個非常緩慢的腳本。任何建議,以加快這一點將不勝感激。Javascript代碼來縮短長文本需要優化

另一個注意:進一步調查後,我看到FF不支持任何類型的CSS格式溢出文本。有一個hack和一個workaround那個黑客......但這不會是一個合適的解決方案。

我已投票支持並加入了mozilla的particular bug的電子郵件列表。它已經快六年了,所以我決定用戶現在只需要處理它。至少這不是我們產品的常見情況。

原帖:

腳本截斷元素的值,並追加「...」,而其scrollWidth大於它的offsetWidth。 (例如,「名字,VeryLongFirstName」的值將改變爲類似「名字,版本...」,取決於列的寬度)

var eTable = document.getElementById(this._eDiv.id + "_tbl"); 

//...lots of code here... 

//function called that gets all cells in a table, loops through them and clips the text 
addEventListenerEx(window, "load", function() {  
     var aCells = eTable.getElementsByTagName("DIV"); 
     window.alert(aCells.length); 
      //When aCells is length of 100, we're ok...but when it's big (like 3,000) I have problems   
     for (var i = 0; i < aCells.length; i++){ 
      Grid.clipText(aCells[i]); 
     } 
}, false); 

//...lots of code here... 

//This is the function doing the actual clipping 
Grid.clipText = function (oDiv) { 

    //for tooltip  
    var oCurDiv; 
    var oTagA; 
    var sToolTip;  
    if (oDiv.firstChild) { 
      if (oDiv.firstChild.firstChild){    
       oCurDiv = oDiv.firstChild; 
       while (oCurDiv) { 
        if (is.ie) { 
         oTagA = oCurDiv;       
        } else { 
         // there are some different between IE & FireFox. 
         oTagA = oCurDiv.firstChild.parentNode;      
        } 
        if (oTagA.tagName == "A") { 
         sToolTip = oTagA.innerHTML;  
         if (sToolTip.indexOf('<b>') > 0) { 
          sToolTip = sToolTip.replace('<b>',""); 
          sToolTip = sToolTip.replace('</b>',""); 
         } 
         if (sToolTip.indexOf('<B>') > 0) { 
          sToolTip = sToolTip.replace('<B>',""); 
          sToolTip = sToolTip.replace('</B>',""); 
         }      
         oTagA.parentNode.title = convertHTMLToText(sToolTip); 
        } 
        oCurDiv = oCurDiv.nextSibling;          
       } 
      } else { 
       oDiv.title = convertHTMLToText(oDiv.innerHTML); 
      } 
     } 

     //NOTE: Additional steps to take for non-IE browsers 
     if (!is.ie) { 
        var oText = oDiv;   
        while (oText.nodeType != 3) { 
         oText = oText.firstChild; 
        } 

        var sDisplayText = oText.nodeValue; 
        if (sDisplayText.length < 3) return; 

        var lastThree; 
        sDisplayText = sDisplayText.slice(0, parseInt(oDiv.offsetWidth/5)); 
        oText.nodeValue = sDisplayText + "..."; 

        //NOTE: Bad things happen here because of this loop 
        while (oDiv.scrollWidth > oDiv.offsetWidth && sDisplayText != "") { 
         lastThree = sDisplayText.slice(-3); 
         sDisplayText = sDisplayText.slice(0, sDisplayText.length - 3); 
         oText.nodeValue = sDisplayText + "..."; 
        } 
        oText.nodeValue = sDisplayText + lastThree.slice(0, 1) + "..."; 
        while (oDiv.scrollWidth > oDiv.offsetWidth && sDisplayText != "") { 
         oText.nodeValue = sDisplayText + "..."; 
        } 
       } 

代碼工作。但是,問題在於,在頁面上加載表格後,它會一遍又一遍地被調用。當桌子很大(> 1,500個單元格)時,問題就開始了。

所以,我真的想找一種方法來使這個示例(特別是WHILE循環)更有效。

回答

0

什麼都不會自行泄漏。你可能在關閉中泄漏oText,你能顯示周圍的代碼嗎?

順便說一句,這裏就是這樣做的遠遠更有效的方式:

http://jsfiddle.net/cwolves/hZqyj/

如果你真的想繼續做,你的方式,你可以通過長度估計分界點該字符串並乘以它所需的比例寬度...

例如如果字符串是100個字符,並且它的長度應爲2倍,則將其剪切爲50個字符並重新檢查。或者你可以實現一個二進制「搜索」算法來獲得正確的長度。

+0

哦...我很想在jQuery中做到這一點! :)唉,我不能。加載並最終調用此代碼的頁面可能有或沒有對jQuery的引用。 – ray 2011-03-24 01:05:14

+1

你可以做到沒有jQuery。 jQuery只做同樣的檢查。這個'魔術'存在於CSS中,只是當孩子長於父母時顯示省略號。 – 2011-03-24 01:06:40

+0

@cwolves ...謝謝。你是對的。我可以。對不起......當我看到「$」時,我很快就會解僱jQuery。我應該更加關注。好的,爲了給出更好的圖片,我添加了更多的代碼。希望能夠更有意義地解釋爲什麼我會遇到這個問題。 – ray 2011-03-24 01:47:25