jQuery 使用 img 作为复选框来显示/隐藏表格列
我需要为用户提供隐藏/显示表列的能力。使用前面的示例和我自己的调整,我使用了复选框来隐藏/显示列,但现在需要删除表单元素。
我对 jquery/javascript 很陌生,所以希望有人可以帮助提供有关如何操作的信息
1.加载时隐藏一些列(基于代码中的触发器)
2. 切换列时交换图像,为用户提供有关显示/隐藏哪些列的视觉反馈。
我有一个小提琴 http://jsfiddle.net/IllusionVK/DqQFP/14/ 具有以下代码其中:
以前使用过以下代码,
$("input:checkbox:not(:checked)").each(function() {
var column = "table ." + $(this).attr("name");
$(column).hide();
});
$("input:checkbox").click(function(){
var column = "table ." + $(this).attr("name");
$(column).toggle();
});
});
但必须删除表单元素(复选框),现在尝试使用 img 标签代替复选框:
$('img').click(function(){
var column = $(this).attr('id');
$('td:nth-child('+ column +')').toggle();
});
我希望最终的代码对于所有无需隐藏/显示列的人来说都是有用的将任何代码添加到实际表中并动态使用 nth-child 。
任何帮助将非常感激!干杯,查理。
到目前为止的帮助来源:
http://www.devcurry。 com/2009/07/hide-table-column-with-single-line-of.html
使用 nth-child,但是是硬编码的,并且仅为一列显示/隐藏而设计
通过使用 jQuery 选中复选框来自动隐藏表格列
但要求表因额外的类而负担过重=对我来说不好,使用复选框=也不好
I need to provide users with the ability to hide/show table columns. Using previous examples with my own adjustments i have used checkboxes to hide/show columns, but now need to remove form elements.
I'm very new to jquery/javascript so hoping someone can help with info on now how to
1. Hide some columns on load (based on trigger in code)
2. Swap images when column is toggled to give users visual feedback as to which columns are shown/hidden.
I have a fiddle at
http://jsfiddle.net/IllusionVK/DqQFP/14/ which has the following code in it:
Previously used following code
$("input:checkbox:not(:checked)").each(function() {
var column = "table ." + $(this).attr("name");
$(column).hide();
});
$("input:checkbox").click(function(){
var column = "table ." + $(this).attr("name");
$(column).toggle();
});
});
but had to remove form elements (checkboxes), and am now trying to use img tag in place of checkbox:
$('img').click(function(){
var column = $(this).attr('id');
$('td:nth-child('+ column +')').toggle();
});
I'm hoping that the final code will be useful for all to hide/show columns without needing to add any code to the actual tables and is using nth-child dynamically.
Any help would be very much appreciated!! Cheers, Charlie.
Sources of help so far:
http://www.devcurry.com/2009/07/hide-table-column-with-single-line-of.html
Uses nth-child, BUT is hardcoded, and designed for only one column show/hide
hide table columns automatically by checking a checkbox with jQuery
But requires that table is overburdened with extra classes=bad for me, use checkboxes=also bad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 index() 如果您制定了约定,则应该能够动态显示/隐藏列您的标签文本与表标题标签相匹配。
更新了 jsfiddle。
附注,img 元素不能包含文本
text
,因此我调整了列表以使用 span 标签。更新
如果您想轻松切换图例项目,您可以执行类似的操作..
以及以下CSS:
并且您的jquery可以切换类以更改背景图像
如果您想在页面加载时隐藏列,只需添加一个取消选中的类即可您想要取消选中的图例 div。
然后运行此脚本将触发取消选中哪些。
jsfiddle 上的示例。
If you use index() you should be able to dynamically show/hide columns if you make a convention that your text of your tags match the table header tags.
Updated jsfiddle.
side note, a img element can't have text
<img>text</img>
, so I adjusted the list to use a span tag instead.update
If you want to easily toggle the legend items you could do something like this..
And the following css:
And your jquery could toggle the class to change the background-image
If you want to hide columns on page load simply add a uncheck class to the legend div you want to uncheck.
Then running this script will trigger which ones to uncheck.
Example on jsfiddle.
我猜想控件类应该没问题。所以我把“复选框”包裹在 a 中,而不是你把它放在... IE 不喜欢里面没有 tbody、tr 或 td 的表格。这就是我所做的(演示):
HTML(只是更新的部分)
CSS(更新部分)
脚本
I was guessing that classes for the controls would be okay. So I wrapped the "checkboxes" in a instead of the you had it in... IE doesn't like tables without a tbody, tr or td inside. So this is what I did (demo):
HTML (just the updated part)
CSS (updated portions)
Script