Ajax加载内容翻倍

时间:2016-10-27 15:11:13

标签: javascript php jquery ajax wordpress

我有一个使用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,'')
            ;
    }

INDEX

            <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>

1 个答案:

答案 0 :(得分:0)

在你的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");之前)