简体   繁体   English

Ajax加载内容翻倍

[英]Ajax load doubles content

I have a site built using wordpress as the cms and the index has a long accordian style list of post titles and once you click on this heading the content for that post is loaded via ajax into the index page. 我有一个使用wordpress构建的网站作为cms,索引有一个长的手风琴样式的帖子标题列表,一旦你点击这个标题,该帖子的内容将通过ajax加载到索引页面。

I have the problem that when the heading is clicked again, it loads the post content again and if you click a different heading it won't close and remove the previous one that has been clicked. 我的问题是,当再次单击标题时,它会再次加载帖子内容,如果单击其他标题,它将不会关闭并删除之前单击的标题。

I am new to working with ajax and unsure how to go about fixing this. 我不熟悉ajax并且不确定如何解决这个问题。 Is there a way to make the content be removed once the heading is clicked a second time to close the accordion or another heading is clicked to expand and open. 有没有办法在第二次单击标题以关闭手风琴或单击另一个标题以展开和打开时删除内容。 Live site is here: http://www.minervasydney.com/ 现场网站在这里: http//www.minervasydney.com/

Below is the code for my ajax and the part in my index file that lists the headings. 下面是我的ajax的代码和我的索引文件中列出标题的部分。 If anyone has ideas it would be greatly appreciated. 如果有人有想法,将不胜感激。 Thank you! 谢谢!

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>

Within your ajax request: 在你的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;
    }           
});

On the second line under success , change .append to .html . 成功的第二行,将.append更改为.html

The line should now be: 这条线现在应该是:

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

It will load the content AND remove previous if there was. 它会加载内容并删除之前的内容。


EDIT 编辑

To close a previously opened .exhibitionDocuments : 要关闭以前打开的.exhibitionDocuments

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

Place it on top of your success callback. 将它置于成功回调之上。
(right before var content = $(data).find(".single-document"); ) (在var content = $(data).find(".single-document");

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

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