6

ajax返回的页面中绑定事件

 3 years ago
source link: https://www.80shihua.com/archives/935
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ajax返回的页面中绑定事件

作者: dreamfly 分类: jquery插件 发布时间: 2016-03-02 12:49

当我们通过ajax返回的值,构造页面或者生成新的元素的时候,默认的事件监听是不起作用的。

$('.next').click(function(){
    alert('点击next按钮');
});

当我们在页面上新生成的按钮

<button class="next">点击我啊</button>

我们点击并不会触发alert()事件,因为这个元素是后添加到dom树上的,我们想要监听这个事件,需要修改我们的代码。

$(document).on('click','.next',function(){
    alert('点击next按钮了');
})

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK