2010-05-23 93 views
0

好吧,這一直在殺死我整夜,我的意思是我已經在這個代碼上工作了至少8個小時了。這是什麼問題,argggg。nextSibling問題

我試圖更新所有<span id="column_[row index number]_[column index number]_[layout position number]">由一個遞增它,直到下一個id="row_[row index number]"tr element,晶體管元件,它應搜索在擁有tr_[row index number]_[column index number]_[layout position number]的ID,但對於一些天知道是什麼原因,它給了我的問題。它正在更新相同的<span>標籤2x,並且這會將其從所需值更改爲比其應該更多的值1 ...在<tr> elements中的每一個的firstChild <td> element內只有1 <span>標籤。我只是不明白爲什麼它只爲其中的一個設置爲2倍,並且它似乎是隨機或其他東西。 argggg。

<td id="tdcolumn_[row index number]_[column index number]_[layout position number]>標籤中只有1 <span>元素,但由於某種原因,它調用相同的標籤兩次。它只應該調用一次。 arggg。我不明白爲什麼?

這裏是我的代碼有人請幫助我在這裏...

// Reorder all columns, if any, in the other rows after this 1. 
if (aRowId != 0 && lId.indexOf("tr_" + aRowId) == 0 && rowComplete != aRowId) 
{ 
    var tempTr = lTable.childNodes[i].childNodes[p]; 


    while(tempTr.nodeType == 1 && tempTr.nextSibling != null) 
    { 
     var tempId = tempTr.getAttribute("id"); 

     if (!tempId) continue; 

     if (tempId.indexOf("row_") == 0) 
     { 
      // All done this row, set it to completed! 
      rowComplete = aRowId; 
      break; 
     } 

     if (tempTr.hasChildNodes) 
     { 

      var doneChilds = false; 

      // grab the id where tdcolumn_{aRowId}.indexOf = 0. 
      for (fcTd = 0; fcTd<tempTr.childNodes.length; fcTd++) 
      { 
       if (tempTr.childNodes[fcTd].nodeName == '#text') continue; 

       var tempfcId = tempTr.childNodes[fcTd].getAttribute("id"); 

       if (!tempfcId) continue; 

       if (tempfcId.indexOf("tdcolumn_" + aRowId) != 0) continue; 

       // looping through the children in the <td> element here. 
       if (tempTr.childNodes[fcTd].hasChildNodes) 
       { 
        for (x = tempTr.childNodes[fcTd].childNodes.length-1; x>0; x--) 
        { 
         if (tempTr.childNodes[fcTd].childNodes[x].nodeName == '#text') continue; 

         var tempSpanId = tempTr.childNodes[fcTd].childNodes[x].getAttribute("id"); 

         if (!tempSpanId) continue; 

         if (tempSpanId.indexOf("column_") != 0) 
          continue; 

         // alert(tempSpanId); 

         alert(tempTr.childNodes[fcTd].childNodes[x].nodeName); 

         var tSpanId = new Array(); 
         tSpanId = tempSpanId.split("_"); 

         if (currColumnId == 0) 
         { 
          currColumnId = parseInt(tSpanId[1]); 
          var incCol = currColumnId; 
         } 

         incCol++; 

         // alert("currColumnId = " + currColumnId + "\n\ntSpanId[1] = " + tSpanId[1] + "\n\nincCol = " + incCol); 

         // Set the new Id's and Text, after which we can exit the for loop. 
         tempTr.childNodes[fcTd].childNodes[x].setAttribute("id", "column_" + incCol); 
         tempTr.childNodes[fcTd].childNodes[x].setAttribute("class", "dp_edit_column"); 
         tempTr.childNodes[fcTd].childNodes[x].innerHTML = oColumnText + " " + incCol; 
         // tempTr.childNodes[fcTd].setAttribute("id", "tdcolumn_" + aRowId + "_" + (parseInt(tSpanId[1])+1) + "_" + tSpanId[3]); 

         doneChilds = true; 

         break; 
        } 
       } 
       else 
        continue; 

       if (doneChilds == true) 
        continue; 
      } 
     } 
     else 
      continue; 

     tempTr = tempTr.nextSibling; 
    } 
} 

請幫幫我,謝謝:)

+0

您可能想用相應的語言標記(Javascript?)標記此標記。 – 2010-05-23 09:06:15

+0

謝謝,是的,我只是... – SoLoGHoST 2010-05-23 09:07:35

+0

@SoLoGHoST:這看起來像是一個轉向JavaScript框架的絕佳機會。這種複雜度級別的DOM操作通過這種方式變得更容易和更便攜。另外 - 您是否嘗試過在瀏覽器的開發人員工具中設置斷點並逐行執行代碼?應該很容易找出調試器有什麼問題... – Tomalak 2010-05-23 09:13:45

回答

2

雖然我不認爲我能解決你的問題,而相關的HTML部分,我看到在你的代碼至少有一個錯誤:

if (doneChilds = true) 

這始終計算爲true。它應該閱讀:

if (doneChilds) 

BTW,你不需要getAttribute這裏:

var tempfcId = tempTr.childNodes[fcTd].getAttribute("id"); 

只需使用:

var tempfcId = tempTr.childNodes[fcTd].id; 

一定不要將使用setAttribute類的名稱,喜歡這裏:

tempTr.childNodes[fcTd].childNodes[x].setAttribute("class", "dp_edit_column"); 

使用:

tempTr.childNodes[fcTd].childNodes[x].className = "dp_edit_column"; 

(這同樣適用於上述的一個行真,設置元素的id)。

+0

您好,Marcel Korpel,如果您希望我可以發送鏈接並訪問論壇的管理員部分,因爲您需要這些才能看到實際的示例。因爲它在論壇的管理部分。將它從那裏拿出是件太多的工作,但我想這可能......讓我知道如果你仍然可以幫助,k?我真的很欣賞這個! :) – SoLoGHoST 2010-05-23 18:50:37