2013-05-17 52 views
1
<script> 
    function hide() 
    { 
     document.getElementById("xxx").style.visibility='visible'; 
    } 
</script> 
<tr> 
    <td>655 3338</td> 
    <td onclick='hide()'>10-May-2013</td>  
</tr> 
<tr id='xxx' style='visibility:collapse'> 
    <td>655 3338</td> 
    <td>10-May-2013</td>   
</tr> 

好日子所有,即時通訊新手在編碼方面的JavaScript語言,即時通訊開發一個簡單的隱藏顯示,上面的代碼(一段代碼)是一個表,當你點擊表格單元格2013年5月10日一些表格行如何顯示,在這種情況下,我是正確的?什麼是我的代碼丟失是當我再次單擊表格單元格10-May-2013它會再次隱藏,或回到其默認樣式(隱藏或摺疊表格)。JavaScript改變風格

回答

1

嘗試

function hide(){ 
    if(document.getElementById("xxx").style.visibility != 'visible'){ 
     document.getElementById("xxx").style.visibility='visible'; 
    } else { 
     document.getElementById("xxx").style.visibility='collapse'; 
    } 
} 

演示:Fiddle

+0

這樣的伎倆在這裏'如果(的document.getElementById( 「XXX」)。style.visibility!= '可見'){'謝謝主席先生,AMH出了問題,爲什麼在JavaScript中,當我嘗試添加例如,第一個文本框的值是1,其他的是2,答案應該是3,但是我的輸出是12,thx對於建議 –

+0

@ RobertjohnConcpcion因爲它做了一個字符串連接,所以你需要將它們轉換爲數字第一個前綴'var a ='1'; var b ='2'; var c = + a ++ b;'http://stackoverflow.com/questions/8976627/how-to-add-two-strings-as-if-they-were-numbers –

0

你可能會更好地切換行的顯示屬性設置爲「無」和「」(空字符串)作爲顯示器的廣泛支持,而且似乎更好地在這裏適用。

例如

<table> 
    <tr><td><button onclick="hideNextRow(this);">Show/Hide next row</button> 
    <tr><td>The next row 
</table> 

<script> 

function hideNextRow(node) { 

    // Get the parent row 
    var row = upTo(node, 'tr'); 

    // If there is one, get the next row in the table 
    if (row) { 
     row = row.parentNode.rows[row.rowIndex + 1]; 
    } 

    // If there is a next row, hide or show it depending on the current value 
    // of its style.display property 
    if (row) { 
     row.style.display = row.style.display == 'none'? '' : 'none'; 
    } 
} 

// Generic function to go up the DOM to the first parent with a 
// tagName of tagname 
function upTo(node, tagname) { 
    var tagname = tagname.toLowerCase(); 

    do { 
    node = node.parentNode; 
    } while (node && node.tagName && node.tagName.toLowerCase() != tagname) 

    // Return the matching node or undefined if not found 
    return node && node.tagName && node.tagName.toLowerCase() == tagname? node : void 0; 
} 
</script>