jquery点击不工作在ajax生成的内容 [英] jquery click doesnt work on ajax generated content

查看:113
本文介绍了jquery点击不工作在ajax生成的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 $(。button)。on(click,function(){});

点击一个容器上的按钮,然后ajax调用完成,内容
更新了新的东西,然后当我尝试点击 .button it wont work ...当我点击按钮时,不会返回任何内容。

to click to a button which is on a container but then an ajax call is done and the content gets updated with new stuff and then when i try to click .button it wont work... nothing will get returned when i click the button.

我尝试了

$(".button").live("click", function(){ });

$(".button").click(function(){ });

如何使其工作?

EDIT:
我的html:

EDIT : my html:

<div class="container">
   <ul>
       <li>item1</li>
       <li>item2</li>
       <li>item3</li>
   </ul>
   <input type="button" value="reload" class="button" />
</div>


推荐答案

应该这样做。

$('body').on('click', '.button', function (){
        alert('click!');
    });

如果您有一个在ajax请求期间不更改的容器, / p>

If you have a container that doesn't change during the ajax request, this is more performant:

$('.container').on('click', '.button', function (){
        alert('click!');
    });

始终将委托事件绑定到将包含动态元素的最接近的静态元素。

Always bind the delegate event to the closest static element that will contain the dynamic elements.

这篇关于jquery点击不工作在ajax生成的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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