问题描述
我试图用jQuery挂钩到gridview中的链接,但网格位于更新面板中,并且在用户运行报表之前不可见。如果我将类.myLink添加到任何其他a标签,它可以正常工作,但由于gridview没有在document.ready中,所以我不确定从哪里调用它。
I am trying to hook into a link in a gridview with jquery but the grid is in an update panel and not visible until the user runs a report. If I add the class ".myLink" to any other "a" tag it works fine, but as the gridview is not there at document.ready I am not sure where to call this from
$(document).ready(function(){
$('a .myLink').click(function(){
var link = $(this).attr('href');
alert(link);
return false;
});
});
推荐答案
您可以使用来处理随时创建的元素上的事件,如下所示:
You can use .live()
to handle events on element created at any time, like this:
$(document).ready(function(){
$('a .myLink').live('click', function(){
var link = $(this).attr('href');
alert(link);
return false;
});
});
如果您有容器出现,并且不取而代之,您可以使用更高效,如下所示:
If you have the container that it'll appear in, and it doesn't get replaced, you can use .delegate()
to be more efficient, like this:
$(document).ready(function(){
$('#containerID').delegate('a .myLink', 'click', function(){
var link = $(this).attr('href');
alert(link);
return false;
});
});
这篇关于如何将jQuery事件附加到在文档准备好时不可见的网格视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!