doujieju0397 2010-10-14 20:09 采纳率: 0%
浏览 28
已采纳

如何编辑在jQuery中显示模式框的链接

I have a problem where I try to show a modal box, it's when I append some a href="" links to my that is on my index page.

I have this in my jQuery script:

$(document).ready(function(){

  $('a').click(function () { 
    // Dialog   
    $("#feed"+$(this).attr('id')).dialog({
      bgiframe: true,
      width: 400,
      modal: true
    });
  return false;
  });
});

And this is the link I have on the index page:

<p><a href="#" id="0">Feed 0</a></p>

If I add the links on the index page it works, but if I empty my div and post new links without any reload they stop to work. No modal box shows, how can I make the jQuery get that the links are there even if they apere after the document is loaded?

I need a jQuery function that allows me to add links after the document has been loaded.

Thanks in Advance Daniel

  • 写回答

1条回答 默认 最新

  • duanluan2047 2010-10-14 20:10
    关注

    Use .live() to attach a handler to document that'll work on current and future links, like this:

    $(function(){
      $('a').live('click', function () { 
        $("#feed"+this.id).dialog({
          bgiframe: true,
          width: 400,
          modal: true
        });
        return false;
      });
    });
    

    As an aside, your id's aren't valid in HTML4 (they can't start with a number, not that they'll realistically cause any issues here, just pointing it out). They are valid in HTML5 however :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3