[英]JQuery hide table column
我试图在加载内容后隐藏 html 表的特定列。 表 html 是动态创建的并使用 JQuery 加载。 这部分按预期工作。
let cur_grid = document.getElementById('grid1') // table html is created. let str_tbl_html = '<table id="tbl_grid1"><tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr></tbody></table>' $.when($(cur_grid).html(str_tbl_html)).done(function() { console.log('hide 3rd column') $('#tbl_grid1 tr td:nth-child(3)').hide() // also tried $('#tbl_grid1').find('td:nth-child(3)').hide() });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='grid1'></div>
我没有收到任何错误,但第 3 列未隐藏。
不要相信 Deferreds 来确定何时将 DOM 元素绘制到屏幕上。 由于您使用的是let
我假设您可以使用现代 JavaScript,例如onanimationstart
。 您可以将其与 CSS 动画一起使用来确定实际绘制表格的时间。
@keyframes any-animation {
from {opacity: 0.99;}
to {opacity: 1.0;}
}
table {
animation-name: any-animation;
animation-duration: 0.001s;
}
let cur_grid = document.getElementById('grid1')
// table html is created.
let str_tbl_html = '<table id="tbl_grid1" onanimationstart="hideThirdColumn()"><tbody><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr></tbody></table>'
function hideThirdColumn() {
$('#tbl_grid1 tr td:nth-child(3)').hide()
};
我在css-tricks.com上的一篇旧博客文章中学到了这个技巧(他在该页面上也提到了其他一些博客作者)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.