Javascript JQuery 委托方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2418298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery delegate method not working
提问by Paul Speranza
I am trying to use the delegate method on a grid that I wrap with the DataTables.Net plug-in. I originally had this code which works as expected.
我试图在我用 DataTables.Net 插件包装的网格上使用委托方法。我最初有这个按预期工作的代码。
$("#myGrid tbody tr").click(function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
However, if I change the paging size then the newer rows don't have the click event calling the function. I decided the new JQuery delegate method should do exactly what I wanted; however, it does nothing at all on any tr element.
但是,如果我更改分页大小,则较新的行没有调用该函数的 click 事件。我决定新的 JQuery 委托方法应该完全符合我的要求;然而,它对任何 tr 元素都没有任何作用。
Could anyone explain why this does not work :
谁能解释为什么这不起作用:
$('#myGrid tbody').delegate('tr', 'click', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
I have tried different combinations of the selector and none get it to work.
我尝试了选择器的不同组合,但没有一个让它起作用。
Thanks Paul Speranza
感谢保罗·斯佩兰萨
采纳答案by Nick Craver
Use this:
用这个:
$("#myGrid tbody tr").live('click', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
.live()works for current of future elements.
.live()适用于当前的未来元素。
回答by Alex Sexton
Try this instead:
试试这个:
$('#myGrid').delegate('tr', 'click', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
There's a good chance that some events on your tbody are getting messed with and/or your tbody's are getting manipulated. I doubt the entire table suffers from this problem as well.
您的 tbody 上的某些事件很可能被弄乱和/或您的 tbody 被操纵。我怀疑整个桌子也有这个问题。
回答by Yossi Shasho
Behind the scenes, bind, delegateand liveall use the method on.
在后台,bind,delegate和live所有使用方法on。
I've had a few problems with delegate, so I started using oninstead.
Converting your delegatecalls to onis easy: just swap the first and second arguments.
我遇到了一些问题delegate,所以我开始使用它on。将您的delegate调用转换on为 很容易:只需交换第一个和第二个参数。
This:
这个:
$('#myGrid tbody').delegate('tr', 'click', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
Becomes this:
变成这样:
$('#myGrid tbody').on('click', 'tr', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
BTW: liveis deprecated in newer version of jQuery
顺便说一句:live在较新版本的 jQuery 中已弃用
回答by Ipog
Try this
尝试这个
$('#myGrid tbody').delegate('click', 'tr', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
or
或者
$('body').delegate('click', '#myGrid tbody tr', function() {
var id = $(this).children('td').eq(0).text();
alert(id);
});
回答by Teja Kantamneni
If the newer rows are being added dynamically, you have to use livemethod on the items, change the delegateto live
如果动态添加较新的行,则必须对live项目使用方法,delegate将live

