在 Tablesorter 中选择行
我在我的项目中使用 Tablesorter 一个 jQuery 插件(我确信其他人知道它)。我需要添加一个功能,当用户单击一行时,它会被选中。我尝试了以下代码,但是它不起作用。
$('#myTable tr').click(function(event) {
$(this).addClass('selected');
});
有人能告诉我最好的方法吗?是否有任何插件可以实现此目的?
预先感谢,
阿卜杜勒·奥拉卡拉
I user Tablesorter a jQuery plugin (I am sure other know about it) in my project. I need to add a feature where in when users click on a row, it gets selected . I tried the following code but, its not working out.
$('#myTable tr').click(function(event) {
$(this).addClass('selected');
});
Could anybody tell me the best way to do it? Is there any plug-in already for this?
Thanks in advance,
Abdel Olakara
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要通过 jQuery 的toggleClass 启用选择和取消选择切换:
To enable select and deselect toggling via jQuery's toggleClass:
这对我来说看起来是正确的。您是否为 tr.selected 定义了 CSS 类?
也许当您单击时,您点击了 td 元素而不是 tr。也许您需要使用父级:
http://docs.jquery.com/Traversing/parent
像这样的(未经测试):
That looks correct to me. Do you have a CSS class defined for tr.selected?
Maybe when you are clicking, you hit the td element and not the tr. Maybe you need to use the parent:
http://docs.jquery.com/Traversing/parent
something like this (untested):
你所拥有的看起来是正确的。您是在文档准备好后执行吗?
或者,您可以使用
live
事件。What you have appears correct. Are you executing it after the document is ready?
Alternatively, you could use
live
events.我认为 Tablesorter 重新创建了整个表,因此这可能就是为什么没有更改的原因,因为它“破坏”了附加到 tr 的单击事件。
您应该使用实时事件进行尝试,看看是否有效:jQuery.com 上的实时事件文档
I think that Tablesorter recreates the whole table so that could be why there is no change as it "destroys" the click event attached to the tr.
You should try it using a live event and see if that works: Documentation for live events at jQuery.com
您的点击事件似乎在我的桌子上运行得很好,我只是想知道您如何通过再次点击来取消选择?将变量与是否被选择绑定似乎是一个简单的解决方案,但我该怎么做呢?
请原谅我用另一个问题来回答你的问题以及我对 JS 的陌生。
Your click event seems to work just fine on my table, I'm just wondering how you would unselect by clicking again? tying a variable to whether is is selected seems like an easy solution, but how would I do that?
Pardon my answering your question with another question and my newness to JS.