阿贾克斯包括网页网页、阿贾克斯

2023-09-10 21:02:00 作者:冰冻的心没有太阳难融化了

我可以做一些功能从阿贾克斯加载网页上? 即。

Can I make some functions on a loaded page from Ajax? I.E.

$(function() {
    $.ajax({
        url: "loaded.php",
        success: function(data) {
            $("#content").html(data);
        }
    });
    $("a#edit").click(function(e) {
        e.preventDefault();
        var newVal = $(this).closest("tr").find("#collapse").html();
        $(this).html(newVal);
    });
});

两个 A#修改 #collapse 是loaded.php的元素,并且该code是在index.php页面......请问这个code的工作?

Both a#edit and #collapse are elements of the loaded.php and this code is in the index.php page... Will this code work?

推荐答案

您有两种选择安装的处理程序中的功能成功后,你的HTML添加到#内容或使用授权。

You have two options attaching the handler in the success function after you add the html to #content or use delegation.

选项1

    success: function(data) {
        $("#content").html(data);
        $("#edit").click(function(e) {
            e.preventDefault();
           var newVal = $(this).closest("tr").find("#collapse").html();
           $(this).html(newVal);
        });
    }

选项2

$(function() {
    $.ajax({
        url: "loaded.php",
        success: function(data) {
            $("#content").html(data);
        }
    });
    $("#content").on('click', '#edit', function(e) {
        e.preventDefault();
        var newVal = $(this).closest("tr").find("#collapse").html();
        $(this).html(newVal);
    });
});

http://api.jquery.com/on/

 
精彩推荐