2013-05-17 115 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>