【问题标题】:jQuery change content of colorboxjQuery更改颜色框的内容
【发布时间】:2011-10-10 19:48:01
【问题描述】:

我已经尝试了类似标题的问题,但它们不起作用。 (例如:How to load AJAX content into current Colorbox window?

我有主页:(包括jQuery 1.6.1)

<script type="text/javascript" src="js/jquery.colorbox.js"></script>
<link rel="stylesheet" type="text/css" href="css/colorbox.css" />
<script>
jQuery(function(){
    $("#aLink").colorbox();
    $('#blub a[rel="open_ajax"]').live('click', function(e){
        e.preventDefault();
        var url = $(this).attr('href'); 
        $.ajax({
            type: 'GET',
            url: url,
            dataType: 'html',
            cache: false,
            beforeSend: function() {
                //$('#cboxContent').empty();
                $('#cboxLoadedContent').empty();
                $('#cboxLoadingGraphic').show();
            },
            complete: function() {
                $('#cboxLoadingGraphic').hide();
            },
            success: function(data) {
                $('#cboxLoadedContent').append(data);

            }
        });
    });
    });
</script>
<a href="1.html" id="aLink">colorbox open</a>

这很好用,1.html 的(简单)内容已加载到颜色框中:

1.html:

<div id="blub">
    <a rel="open_ajax" href="2.html">Change Content</a>
</div>

现在我想通过单击链接来更改内容。这行不通。 以太我得到一个额外的颜色框,或者什么也没有发生。

谢谢!

【问题讨论】:

  • 您的意思是当颜色框打开时(内容来自 1.html),它有指向 2.html 的链接?所以基本上,你想要一个颜色框内的链接来更改颜色框的内容。对吗?
  • 1.html的内容加载到colorbox中。 “更改内容”链接已经存在。我点击链接...现在我希望将 2.html 的内容加载到现有的颜色框中

标签: jquery-plugins colorbox


【解决方案1】:

您可以使用 jquery live() 函数来监视对现有和未来颜色框链接的点击。另外,我建议您不要使用 rel 作为您的选择器。该属性旨在用于 SEO。所以在这个例子中,我把你的选择器从 rel 属性移到了 class 属性:

$(document).ready(function() {
    $('a.open_ajax').live('click', function(){
        $.colorbox({
            open:true,
            href: this.href
            //plus any other interesting options    
        });

        return false;
    });
});

然后只需确保您想要触发新颜色框内容的任何内容都具有“open_ajax”类和一个 href。例如:

&lt;a class="open_ajax" href="colorboxPage.html"&gt;Open&lt;/a&gt;

jQuery 1.7+ 更新

对于 jQuery 1.7,由于 live() 已被弃用,您需要这样做:

$(document).on("click", "a.open_ajax", function() {
    //everything that was in the live() callback above
});

【讨论】:

  • 嗨,这不起作用。现在 2.html 的内容在第二个颜色框中(在第一个颜色框之后)
  • 是的。我会用我使用的完整代码更新它。它不会打开新的颜色框,而是将内容加载到其中然后调整现有颜色框的大小。
  • 我真的很抱歉,但这不是我可以在快速评论中回​​答的问题,它也不适合这个答案。我得走了。但这是一个非常好的问题。你应该开始一个新的问题,如果没有人在我之前得到它,那么我会考虑一下。 PS-您在我完成之前编辑了您的评论并删除了您的问题,但实际上,您应该问它。我相信它会是很好的搜索引擎。
  • 你好,后续在这里:stackoverflow.com/questions/7723062/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多