jQuery AJAX 文档触发器?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在这里使用
.live()
,如下所示:目前不起作用,因为
$('.Img')
找不到要附加click
处理程序的。 ..它当时并不存在,直到ajax调用加载它为止,
.live()< /code>
将适当地侦听单击,即使该元素是稍后添加的。
You need to use
.live()
here, like this:It doesn't work currently because
$('.Img')
doesn't find the<img>
to attach aclick
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.