jQuery AJAX 文档触发器?

发布于 2024-09-07 08:46:20 字数 582 浏览 1 评论 0原文

我的 jQuery 代码:

$('.Img').click(function() {
    alert('Test');
});

$().ready(function() {
    $.ajax( {
        type     : 'POST',
        url      : 'Post.php',
        success  : function(Response) {
          $('#Response').html(Response);
        }
    }
});

我的 HTML 代码:

<div id="Response"></div>
<img class="Img" src="blank.gif" /> [Click Trigger]

我的 PHP 代码:

echo '<img class="Img" src="blank.gif" />'; [Ajax from response]

为什么该图像不会从 AJAX 响应触发?

My jQuery code:

$('.Img').click(function() {
    alert('Test');
});

$().ready(function() {
    $.ajax( {
        type     : 'POST',
        url      : 'Post.php',
        success  : function(Response) {
          $('#Response').html(Response);
        }
    }
});

My HTML code:

<div id="Response"></div>
<img class="Img" src="blank.gif" /> [Click Trigger]

My PHP code:

echo '<img class="Img" src="blank.gif" />'; [Ajax from response]

why this image does not trigger from AJAX response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-09-14 08:46:20

您需要在这里使用 .live() ,如下所示

$('.Img').live('click', function(){
 alert('Test');
});

:目前不起作用,因为 $('.Img') 找不到要附加 click 处理程序的 。 ..它当时并不存在,直到ajax调用加载它为止, .live()< /code>将适当地侦听单击,即使该元素是稍后添加的。

You need to use .live() here, like this:

$('.Img').live('click', function(){
 alert('Test');
});

It doesn't work currently because $('.Img') doesn't find the <img> to attach a click handler to...it didn't exist then, not until the ajax call loaded it, .live() will listen for the click appropriately, even if the element is added later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文