繁体   English   中英

Ajax加载内容翻倍

[英]Ajax load doubles content

我有一个使用wordpress构建的网站作为cms,索引有一个长的手风琴样式的帖子标题列表,一旦你点击这个标题,该帖子的内容将通过ajax加载到索引页面。

我的问题是,当再次单击标题时,它会再次加载帖子内容,如果单击其他标题,它将不会关闭并删除之前单击的标题。

我不熟悉ajax并且不确定如何解决这个问题。 有没有办法在第二次单击标题以关闭手风琴或单击另一个标题以展开和打开时删除内容。 现场网站在这里: http//www.minervasydney.com/

下面是我的ajax的代码和我的索引文件中列出标题的部分。 如果有人有想法,将不胜感激。 谢谢!

AJAX

        $("a.exhibitionInformation").on("click", function(event) {

        var exhibitionContainer = $(this).parent(".exhibition");
        var url = $(this).attr("href");
        var doc = $(this).next(".exhibitionDocuments");
        var title = convertToSlug($(this).text().split("\n")[1]);
        var slug = $(this).attr("data-slug");
        $(this).toggleClass("selected");

        if($(doc).is(':not(:hidden)')) {
            $(doc).slideToggle();
        } else {            


            $.ajax({
                url: url,
                type: "GET",
                success: function(data) {
                    var content = $(data).find(".single-document");                 
                    $(doc).append(content).slideToggle(300);                    
                    $("html, body").animate({ scrollTop: exhibitionContainer.offset().top - 26 });
                    window.location.hash = slug;
                }           
            });

        }

        event.preventDefault();
    });


    //ajaxstarStop Loading
    $( document ).ajaxStart(function() {
        $( "#loading" ).fadeIn(100);
    });

    $( document ).ajaxStop(function() {
        $( "#loading" ).fadeOut();
    });     


    function convertToSlug(Text)
    {
        return Text
            .toLowerCase()
            .replace(/ /g,'-')
            .replace(/[^\w-]+/g,'')
            ;
    }

指数

            <section class="exhibition">

            <a class="exhibitionInformation" href="<?php the_permalink(); ?>" data-slug="<?php echo $post->post_name; ?>">
                <span class="dates"><?php echo types_render_field("dates", array("argument1"=>"value1")); ?></span>
                <span class="opening"><?php echo types_render_field("opening", array("argument1"=>"value1")); ?></span>
                <span class="title">&#8220;<?php the_title(); ?>&#8221;</span>
                <span class="artist"><?php echo types_render_field("artist", array("argument1"=>"value1")); ?></span>
                <span class="extra"><?php echo types_render_field("extra", array("argument1"=>"value1")); ?></span>
            </a>


            <article class="exhibitionDocuments">

            </article>

        </section>

在你的ajax请求中:

$.ajax({
    url: url,
    type: "GET",
    success: function(data) {
        var content = $(data).find(".single-document");                 
        $(doc).append(content).slideToggle(300);                    
        $("html, body").animate({ scrollTop: exhibitionContainer.offset().top - 26 });
        window.location.hash = slug;
    }           
});

成功的第二行,将.append更改为.html

这条线现在应该是:

$(doc).html(content).slideToggle(300);

它会加载内容并删除之前的内容。


编辑

要关闭以前打开的.exhibitionDocuments

 $(document).find(".exhibitionDocuments").hide(); 

将它置于成功回调之上。
(在var content = $(data).find(".single-document");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM