2012-06-26 36 views
2

我試圖讓錶行崩潰,如果它們不包含數據。行從JQuery腳本中更改。我知道如果我把表格放在我的HTML中,它們會崩潰。我可以創建多行並使用display:blockdisplay:none,並按預期方式摺疊/顯示行。下面是標記:錶行在HTML中摺疊,但不是從JQuery腳本中摺疊

<table style="border-collapse:collapse;"> 
    <tr style="display:block;"> 
     <td id="someid">Test</td> 
     <td>Some text</td> 
    </tr> 
</table> 

下面是示例代碼我使用改變<tr>:改變行

var newtr = "<tr style='display:none;'><td id='someid'></td><td></td></tr>" 
$("td#someid").parent().replaceWith(newtr); 

的內容,但他們不崩潰。我正在使用Firebug查看正在插入display:none。還有什麼我失蹤?

回答

4

爲什麼不只是添加hide()

$("td#someid").parent().replaceWith(newtr).hide(); 

Working example here

+2

爲什麼?因爲我不知道我能做到這一點,所以很容易。 :) 我喜歡這個。我不太瞭解JQuery/Javascript。謝謝! – rwkiii

+0

@rwkiii閱讀[jQuery教程在這裏](http://docs.jquery.com/Tutorials)和[JavaScript教程在這裏](https://developer.mozilla.org/en/JavaScript/Guide) – ManseUK

1

,而不是替換tr的; 如果要清除的字段和隱藏的行,我建議乾脆躲在行,然後清除細胞之後,這樣的事情應該做的伎倆:

// this will explicitly set the CSS display property to none and clear the children 
$('td#someid').parent().css('display', 'none').children('td').each(function() 
{ 
    $(this).text(''); 
}); 

// or you could hide the parent and then clear the children 
$('td#someid').parent().hide().children('td').each(function() 
{ 
    $(this).text(''); 
}); 
+0

使用'.hide'無疑爲我增加了一些功能。我不明白爲什麼我會隱藏該行,並將CSS設置爲「display:none」。你們兩個都給了我很好的答案。替換行是在創建JSON對象的XSLT樣式表中生成的。你的回答讓我考慮簡化樣式表中的一些代碼。謝謝理查德。 – rwkiii

+0

您不需要使用這兩個示例,只需要/或。我肯定會推薦使用CSS來簡化事情。 – Richard