2010-04-22 230 views
0

我有一些動態創建的行/列。我想要做的是設置它的一部分(txtOffsetID)隱藏。我試過這個:txtOffsetID.setAttribute('type', 'hidden');但它沒有工作。我想隱藏整個列和添加的新列。我需要它在IE中工作。謝謝。隱藏動態添加的列/字段

示例代碼:

function addNewOffsetItem() 
{ 
    var iX = document.getElementById("txtOffsetIndex").value; 
    iX ++; 
    document.getElementById("txtOffsetIndex").value = iX; 

    var tbl = document.getElementById("tblOffsetDetail").getElementsByTagName("TBODY")[0]; 
    var tr = document.createElement("TR"); 
    tbl.appendChild(tr); 

    //This section should be hidden.  
    //txtOffsetID1 
    var tdID = document.createElement("TD"); 
    tr.appendChild(tdID); 

    var p = document.createElement("P"); 
    tdID.appendChild(p); 

    var txtOffsetID = document.createElement("input"); 
    p.appendChild(txtOffsetID); 

    txtOffsetID.id = "txtOffsetID" + iX; 
    txtOffsetID.setAttribute('name','txtOffsetID' + iX); 
    **document.getElementById("colOffsetID").style.display="none";** 

    //This section should be visible. 
    //txtOffsetComments1 
    var tdComments = document.createElement("TD"); 
    tr.appendChild(tdComments); 

    var p = document.createElement("P"); 
    tdComments.appendChild(p); 

    var txtOffsetComments = document.createElement("textarea"); 
    p.appendChild(txtOffsetComments); 

    txtOffsetComments.id = "txtOffsetComments" + iX; 
    txtOffsetComments.setAttribute('name','txtOffsetComments' + iX);  
} 

<table width="99%" border="1" cellpadding="1" cellspacing="1" id="tblOffsetDetail"> 
    <colgroup> 
    <col id="colOffsetID"> 
    <col id="colOffsetComments"> 
    </colgroup> 
    <tbody>  
     <tr> 
      <td><input type="text" id="txtOffsetID" name="txtOffsetID"></td> 
      <td><p><textarea name="txtOffsetComments" cols="15" rows="3" id="txtOffsetComments"></textarea></p></td>   
     </tr> 
    </tbody> 
</table> 

回答

1

創建「TR」元素可以被隱藏這樣

 
tr.setAttribute('style', 'display: none;'); 

,但如果你想比你需要使用COLGROUP表元素
隱藏列滿示例

 
<table> 
<colgroup> 
<col id="colOne"> 
<col id="colTwo"> 
<col id="colThre"> 
</colgroup> 
<tbody> 
<tr> 
<td>one</td> 
<td>two</td> 
<td>three</td> 
</tr> 
</tbody> 

您可以隱藏colTwo li柯本

 
document.getElementById('colTwo').style.display = 'none'; 

我希望這有助於

+0

這個工作。謝謝! – SeanFlynn 2010-04-22 23:46:42