最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Colorbox and Content returned via ajax - Stack Overflow

programmeradmin1浏览0评论

I'm using jquery colorbox to popup user accounts in a window. I also have a button that loads more users into the page via ajax, but for some reason users loaded with ajax do not popup in colorbox window. How can I get colorbox to work with the content that was returned via ajax?

function loadMore(pageNo) {
    //$("#loading").html('Loading...').show();
    var url = '/search/resultsAjax';
    $.get(url, {page: pageNo}, function(response) {
        $("#results").append(response);
        //$("#loading").hide();
    });
}

$(document).ready(function() {
    var currPage = 1;
    $("a.next").click(function() {
        loadMore(++currPage);
    });

    $("a.usr").colorbox({width:"960px", height:"90%", iframe:true});
});

I'm using jquery colorbox to popup user accounts in a window. I also have a button that loads more users into the page via ajax, but for some reason users loaded with ajax do not popup in colorbox window. How can I get colorbox to work with the content that was returned via ajax?

function loadMore(pageNo) {
    //$("#loading").html('Loading...').show();
    var url = '/search/resultsAjax';
    $.get(url, {page: pageNo}, function(response) {
        $("#results").append(response);
        //$("#loading").hide();
    });
}

$(document).ready(function() {
    var currPage = 1;
    $("a.next").click(function() {
        loadMore(++currPage);
    });

    $("a.usr").colorbox({width:"960px", height:"90%", iframe:true});
});
Share Improve this question asked Jan 26, 2011 at 2:56 BugBusterXBugBusterX 2031 gold badge3 silver badges12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

If you want to make it work globally, regardless of how the content is loaded:

$('body').on('click', 'a.usr', function() {
    $(this).colorbox({width:"960px", height:"90%", iframe:true});
});

When you bind colorbox in $(document).ready() with this:

$("a.usr").colorbox({width:"960px", height:"90%", iframe:true});

jQuery will go search through the page, grab everything that matches a.usr, bind colorbox to those items, and then forget all about a.usr. jQuery won't remember that you're interested in a.usr so the new ones won't be colorbox'd.

The usual solution to this sort of thing is to use .live() or .delegate() but those won't work here as there isn't a "colorbox" event.

However, you can bind colorbox by hand easily enough. Try change loadMore to something more like this:

function loadMore(pageNo) {
    $.get('/search/resultsAjax', { page: pageNo }, function(response) {
        var $html = $(response);
        $html.find('a.usr').colorbox({ width: '960px', height: '90%', iframe: true });
        $('#results').append($html);
    };
}

You just turn the response HTML into a jQuery object, find all the a.usr things inside it, bind colorbox to those, and then insert the new HTML into the DOM as usual.

Use this:

<a onclick='parent.$.colorbox({href:"schedule_delete.php?sid=<?php echo $schedule_row->schedule_id; ?>"}); return false;'>delete</a
发布评论

评论列表(0)

  1. 暂无评论