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