如何将jQuery事件附加到在文档准备好时不可见的网格视图? [英] How to attach a jQuery event to a grid view that is not visible on document ready?

查看:236
本文介绍了如何将jQuery事件附加到在文档准备好时不可见的网格视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用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;
                });
              });


推荐答案

您可以使用 .live() 来处理随时创建的元素上的事件,如下所示:

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;
  });
});

如果您有容器出现,并且取而代之,您可以使用 .delegate() 更高效,如下所示:

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事件附加到在文档准备好时不可见的网格视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆