[英]Hiding HTML table columns with JavaScript
I want to hide certain columns of a HTML template with JavaScript. 我想用JavaScript隐藏HTML模板的某些列。 Could anybody tell me how can I achieve this? 有人可以告诉我如何实现吗? I've tried giving IDs to the entire column TDs and then making document.getElementById("ID").style.display = "none"
but it hides only the first TD. 我尝试将ID赋予整个列TD,然后使document.getElementById("ID").style.display = "none"
但它仅隐藏第一个TD。
Thank you. 谢谢。
Have you considered using the jQuery Library ? 您是否考虑过使用jQuery库 ?
$('.tdCssClass').hide()
You could even go one step further and avoid the use of a cssClass: 您甚至可以更进一步,避免使用cssClass:
$('td:nth-child(2)').hide()
Would hide the 2nd column. 将隐藏第二列。
IDs are only supposed to be used uniquely, ie for one element only, therefore getElementById
will only return 1 element, the first that matches. ID仅应唯一地使用,即仅用于一个元素,因此getElementById
将仅返回1个元素,第一个匹配。 Try using a class instead and: 尝试改用一个类,然后:
var elems = document.getElementsByClassName("someClass");
for(var i = 0; i < elems.length; i++)
elems[i].style.display = "none";
EDIT: in jQuery mode on accident >:) 编辑:在jQuery模式下发生意外> :)
我最近使用了jquery columnManager http://p.sohei.org/jquery-plugins/columnmanager/来允许用户轻松显示/隐藏表列,并且效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.