2011-08-18 94 views
2

我有一個帶有onclick函數的表。當點擊時,我得到以下錯誤: Uncaught TypeError: Cannot set property 'className' of undefinedJavascript:Uncaught TypeError:無法設置未定義的屬性'className'

奇怪的是,5行中有3行class被更改,但2/5不是。當再次點擊該行時,其餘兩個中的一個被更改,並且當再次單擊時,最後一個被更改。這裏是我的代碼:

HTML表格

<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '6', '5')"><td><input type='checkbox' name='strFormFactuur[]' value='19500' id='chk6'>&nbsp;<img src='../images/link.png' border='0'></td>  
    <td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('6', '?iID=19500&bType=0', 'all');")> <input type='image' src='../images/toevoegen.png' border='0' style='float: right; cursor: pointer;' name='FormDupliceer' value='19500'></td>  
</tr> 
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '7', '5')"><td></td>  
    <td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('7', '?iID=36&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='36'></td>  
</tr> 
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '8', '5')"><td></td>  
    <td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('8', '?iID=37&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='37'></td>  
</tr> 
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '9', '5')"><td></td>  
    <td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('9', '?iID=38&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='38'></td>  
</tr> 
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '10', '5')"><td></td>  
    <td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('10', '?iID=66&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='66'></td>  
</tr> 

JavaScript函數FormTRClick()

function FormTRClick(event, uID, PCID) { 
    var Object= event.target.tagName; 
    if (Object == 'TD') { 
     var Elements = document.getElementsByClassName('row' + PCID); 
     var iNumElements = Elements.length; 
     alert(iNumElements); // Outputs 5 as expected 
     for (var i=0; i < iNumElements; i++) { 
      Elements[i].className = "invoice-tr-clicked"; // Only changes 3 out of 5 on the first click   
     } 
    } 
} 

更新

當使用以下Elements[i].className += " invoice-tr-clicked";而不是Elements[i].className = "invoice-tr-clicked";,腳本作品如這應該。但是,當更換(因此=)我得到Cannot set property 'className' of undefined錯誤。

+0

我試圖在這裏重新構建您的示例:http://jsbin.com/ugojaj/edit#javascript,html 但我無法重現錯誤。 你能提供一個可用的SSCCE嗎? – Ben

+0

@Ben我已經改變了一些代碼,它按預期工作,但不在我的環境中。此代碼的作品:http://jsbin.com/ugojaj/2我必須玩弄它。 – Devator

+0

我已更新我的帖子。 – Devator

回答

1

我用

Elements[i].className = "invoice-tr-clicked row" + PCID; 

更換

Elements[i].className = "invoice-tr-clicked"; 

不使現在的工作任何意義,因爲demo作品沒有問題固定它。

相關問題