ajax内容加载后点击不工作 [英] Click not working after ajax content has loaded

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

问题描述

如何在 ajax 调用后使 live('click',function()" 工作并返回带有 html(data) 的内容?经过 4 小时的搜索,我想我最好问一下,因为我无处可去.

How can I make live('click',function()" work after an ajax call and returning content with html(data)? After 4 hrs of searching I think I'd better ask because I am getting nowhere.

这部分正在工作:

$.ajax({
    type: "POST",
    url: "loadAlbum.php",
    data: dataString,
    success: function(data){
        $(".loader").html(data, {}, function(){ 
        //content is loaded now
        //need to get the function below working in this part of the content
        }); 
    },
    error : function(data) { } 
    });
});

我需要这个在上面的 ajax 中工作:

And I need this one to work in the ajax above:

$('.divName as inside loader').live('click',function(){  

alert('gotClicked');

    var vidItem = $(this).attr('data');
    vidItem = vidItem.split('-'); var bookID = vidItem[0]; var state = vidItem[1];
    var dataString = 'bookID='+ bookID + '&state=' + state;

alert(dataString); 

});

推荐答案

.live() 已弃用.改用 .on().

$("body").on("click", "divClass", function(event){
    alert('gotClicked');
});

此外,为了确保您正确调用了 div,在以这种方式调用它时,它不应该是 div 名称,而应该是 div 类.

Also, just to make sure you're calling the div correctly, it shouldn't be the div name it should be the div class when calling it in that fashion.

此外,使用 live()on() 需要在文档加载时存在的父级应用.如果页面加载本身时您使用的这个 divName 不存在,则无法绑定到它.但是,如果您绑定到 body 元素,那么它会在发生点击时始终查找 div.

Also, using live() and on() needs to be applied at a parent level that exists at the document load. If this divName you're using didn't exist when the page loaded itself it can't be bound to. However, if you bind to the body element, then it'll always look for the div when the click occurs.

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

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