Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript colorbox发送到错误的url?_Javascript_Jquery_Ajax_Colorbox - Fatal编程技术网

Javascript colorbox发送到错误的url?

Javascript colorbox发送到错误的url?,javascript,jquery,ajax,colorbox,Javascript,Jquery,Ajax,Colorbox,我在一页中有多个颜色框 这些colorbox的链接是用javascript生成的 现在,每次生成打开colorbox的新链接时,与colorbox关联的任何单击链接都将转到该新链接的url 下面是我如何声明colorbox的: $('.ajax').colorbox(); 链接中的href是正确的,但仍然发送到最后生成的链接的href 提前谢谢 编辑: 这是我如何添加一些链接的代码: $('.albums').live('click', function(e){ e.preventDe

我在一页中有多个颜色框

这些colorbox的链接是用javascript生成的

现在,每次生成打开colorbox的新链接时,与colorbox关联的任何单击链接都将转到该新链接的url

下面是我如何声明colorbox的:

$('.ajax').colorbox();
链接中的href是正确的,但仍然发送到最后生成的链接的href

提前谢谢

编辑: 这是我如何添加一些链接的代码:

$('.albums').live('click', function(e){
    e.preventDefault();
    e.stopPropagation();
    parent_id           = $(this).parents().eq(2)[0].id;
    parent_header       = $('#' + parent_id + ' header')[0].innerHTML;
    parent_section      = $('#' + parent_id + ' section')[0].innerHTML;
    parent_footer       = $('#' + parent_id + ' footer')[0].innerHTML;
    fragment            = $(this).attr('href').split('/')[$(this).attr('href').split('/').length - 1];
    parent              = $(this).parents().eq(2)[0].id;
    if(fragment.indexOf('_-_') !== -1)
        page = fragment.replace('_-_', '/');
    else
        page = fragment;
    old_data[parent_id] = {'header': parent_header, 'footer': parent_footer, 'section': parent_section, 'link': parent + '/' + page};
    $.post('files/get_page', { page: old_data[parent_id]['link'] }, function(data){
        ret     = JSON.parse(data);
        a       = $('<a>').addClass('reset_link').attr('href', '#').append('Back To Albums');
        a.click(function(e){
            e.preventDefault();
            e.stopPropagation();
            this_parent_id      = $(this).parents().eq(1)[0].id;
            $('#' + this_parent_id + ' header').html(old_data[this_parent_id]['header']);
            $('#' + this_parent_id + ' section').html(old_data[this_parent_id]['section']);
            $('#' + this_parent_id + ' footer').html(old_data[this_parent_id]['footer']);
        })
        $('#' + parent).children().eq(0).html(a);
        $('#' + parent).children().eq(0).append(ret.header);
        $('#' + parent).children().eq(1).html('');
        for(i in ret.body.images){
            href            = ret.body.href + ret.body.images[i].image_name;
            image_id        = ret.body.images[i].image_id;
            delete_div_id   = 'delete_image_' + image_id;
            body            = $('<div>').addClass('albums_div');
            delete_div      = $('<div>').addClass('delete').append('&#10006; Supprimer').attr('id', image_id);
            a               = $('<a>').addClass('lightbox').attr('href', href);
            a.attr('onclick', 'return false;');
            a.append('&#9673; ' + ret.body.images[i].image_name);
            delete_div.click(function(){
                id          = delete_div_id;
                id_array    = id.split('_');
                $.post('files/delete', { id: image_id, del: id_array[1] }, function(data){
                    $(body).remove();
                });
            });
            body.append(a);
            body.append(delete_div);
            $('#' + parent).children().eq(1).append(body);
            $('.lightbox').colorbox({rel: 'group1'});
        }

        footer  = $('<a>').addClass(ret.footer['attr'].class).attr('id', ret.footer['attr'].id).attr('href', ret.footer['href']);
        footer.append(ret.footer['text']);
        footer.click(function(e){
            e.preventDefault();
            $.colorbox({href: ret.footer['href']});
        });
        $('#' + parent).children().eq(2).html(footer);
    });
});
并且打印到控制台的href是正确的,但是颜色框中显示的页面是错误的

编辑2添加HTML:

javascript更改代码之前的页面:

<section id="reference">
    <header class="inner_header"><h3>R&#201;F&#201;RENCE PAGE SECTION</h3></header>
    <section>
        <h4>Choose an album :</h4>
        <div class="albums_div">
            <a href="res_yass" onClick="return false;" class="albums">&#9733; R&#233;sidence YASSINE</a>
            <div class="delete" id="delete_album_1">&#10006; Supprimer</div>
        </div>
        <div class="albums_div">
            <a href="res_nesr" onClick="return false;" class="albums">&#9733; R&#233;sidence NESRINE</a>
            <div class="delete" id="delete_album_2">&#10006; Supprimer</div>
        </div>
        <div class="albums_div">
            <a href="azerty" onClick="return false;" class="albums">&#9733; Azerty</a>
            <div class="delete" id="delete_album_8">&#10006; Supprimer</div>
        </div>
    </section>
    <footer class="inner_footer"><a href="files/add/reference" class="add_album ajax" id="reference">Add an album</a></footer>
</section>
<section id="reference">
    <header class="inner_header"><a class="reset_link" href="#">Back To Albums</a><h4>Azerty</h4></header>
    <section>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f352702127.jpg" onclick="return false;">◉ f352702127.jpg</a>
            <div class="delete" id="128">✖ Supprimer</div>
        </div>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f65564287.jpg" onclick="return false;">◉ f65564287.jpg</a>
            <div class="delete" id="129">✖ Supprimer</div>
        </div>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f184021055.jpg" onclick="return false;">◉ f184021055.jpg</a>
            <div class="delete" id="130">✖ Supprimer</div>
        </div>
    </section>
    <footer class="inner_footer"><a class="upload_file ajax" id="azerty" href="uploader/get_form/reference/azerty">Upload une image</a></footer>
</section>

RÉ;FÉ;第页
选择相册:
✖ 供给者
✖ 供给者
✖ 供给者
javascript更改代码后的页面:

<section id="reference">
    <header class="inner_header"><h3>R&#201;F&#201;RENCE PAGE SECTION</h3></header>
    <section>
        <h4>Choose an album :</h4>
        <div class="albums_div">
            <a href="res_yass" onClick="return false;" class="albums">&#9733; R&#233;sidence YASSINE</a>
            <div class="delete" id="delete_album_1">&#10006; Supprimer</div>
        </div>
        <div class="albums_div">
            <a href="res_nesr" onClick="return false;" class="albums">&#9733; R&#233;sidence NESRINE</a>
            <div class="delete" id="delete_album_2">&#10006; Supprimer</div>
        </div>
        <div class="albums_div">
            <a href="azerty" onClick="return false;" class="albums">&#9733; Azerty</a>
            <div class="delete" id="delete_album_8">&#10006; Supprimer</div>
        </div>
    </section>
    <footer class="inner_footer"><a href="files/add/reference" class="add_album ajax" id="reference">Add an album</a></footer>
</section>
<section id="reference">
    <header class="inner_header"><a class="reset_link" href="#">Back To Albums</a><h4>Azerty</h4></header>
    <section>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f352702127.jpg" onclick="return false;">◉ f352702127.jpg</a>
            <div class="delete" id="128">✖ Supprimer</div>
        </div>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f65564287.jpg" onclick="return false;">◉ f65564287.jpg</a>
            <div class="delete" id="129">✖ Supprimer</div>
        </div>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f184021055.jpg" onclick="return false;">◉ f184021055.jpg</a>
            <div class="delete" id="130">✖ Supprimer</div>
        </div>
    </section>
    <footer class="inner_footer"><a class="upload_file ajax" id="azerty" href="uploader/get_form/reference/azerty">Upload une image</a></footer>
</section>

阿泽蒂
✖ 供给者
✖ 供给者
✖ 供给者

解决了,问题不在于颜色盒,我只是从以下代码中提取代码:

$('.albums').live('click', function(e){
    e.preventDefault();
    e.stopPropagation();
    parent_id           = $(this).parents().eq(2)[0].id;
    parent_header       = $('#' + parent_id + ' header')[0].innerHTML;
    parent_section      = $('#' + parent_id + ' section')[0].innerHTML;
    parent_footer       = $('#' + parent_id + ' footer')[0].innerHTML;
    fragment            = $(this).attr('href').split('/')[$(this).attr('href').split('/').length - 1];
    parent              = $(this).parents().eq(2)[0].id;
    if(fragment.indexOf('_-_') !== -1)
        page = fragment.replace('_-_', '/');
    else
        page = fragment;
    old_data[parent_id] = {'header': parent_header, 'footer': parent_footer, 'section': parent_section, 'link': parent + '/' + page};
    $.post('files/get_page', { page: old_data[parent_id]['link'] }, function(data){
        ret     = JSON.parse(data);
        a       = $('<a>').addClass('reset_link').attr('href', '#').append('Back To Albums');
        a.click(function(e){
            e.preventDefault();
            e.stopPropagation();
            this_parent_id      = $(this).parents().eq(1)[0].id;
            $('#' + this_parent_id + ' header').html(old_data[this_parent_id]['header']);
            $('#' + this_parent_id + ' section').html(old_data[this_parent_id]['section']);
            $('#' + this_parent_id + ' footer').html(old_data[this_parent_id]['footer']);
        })
        $('#' + parent).children().eq(0).html(a);
        $('#' + parent).children().eq(0).append(ret.header);
        $('#' + parent).children().eq(1).html('');
        for(i in ret.body.images){
            href            = ret.body.href + ret.body.images[i].image_name;
            image_id        = ret.body.images[i].image_id;
            delete_div_id   = 'delete_image_' + image_id;
            body            = $('<div>').addClass('albums_div');
            delete_div      = $('<div>').addClass('delete').append('&#10006; Supprimer').attr('id', image_id);
            a               = $('<a>').addClass('lightbox').attr('href', href);
            a.attr('onclick', 'return false;');
            a.append('&#9673; ' + ret.body.images[i].image_name);
            delete_div.click(function(){
                id          = delete_div_id;
                id_array    = id.split('_');
                $.post('files/delete', { id: image_id, del: id_array[1] }, function(data){
                    $(body).remove();
                });
            });
            body.append(a);
            body.append(delete_div);
            $('#' + parent).children().eq(1).append(body);
            $('.lightbox').colorbox({rel: 'group1'});
        }

        footer  = $('<a>').addClass(ret.footer['attr'].class).attr('id', ret.footer['attr'].id).attr('href', ret.footer['href']);
        footer.append(ret.footer['text']);
        footer.click(function(e){
            e.preventDefault();
            e.stopPropagation();
            $.colorbox({href: ret.footer['href']});
        });
        $('#' + parent).children().eq(2).html(footer);
    });
});
因此,每次重写页脚变量时,单击会被重新分配到新页脚,这就是为什么colorbox会将数据发送到错误的url,所以我所要做的就是将这些变量设置为函数的局部变量。在这种情况下,swap_content()函数现在每个页脚都是唯一的,因此colorbox会将数据发送到正确的url。 感谢那些试图回答问题的人。
希望这对任何人都有帮助。

需要更多信息。像代码或其他东西,这将需要很长时间来准备,因为它集成在一个几乎完整的网站中。问题是,当生成新链接时,colorbox会在每次单击任何其他链接时获取该链接的href。它不应该这样做,但它确实这样做了。这里有一些代码希望它能澄清我的问题所在。@RicardoLohmann刚刚更正了html
footer.click(function(e){
    e.preventDefault();
    e.stopPropagation();
    $.colorbox({href: ret.footer['href']});
});