简体   繁体   English

Ajax Div切换避免重新加载内容

[英]Ajax Div toggle avoiding reloading content

I had a question regarding Ajax loading of html into a DIV. 我有一个关于将Ajax将html加载到DIV中的问题。 Ideally what I want is this: 理想情况下,我想要的是:

A toggle div with close button, which I have the code for here: http://jsfiddle.net/tymeJV/uhEgG/28/ 带关闭按钮的切换div,我在这里有代码: http : //jsfiddle.net/tymeJV/uhEgG/28/

$(document).ready(function () {
    $('#country').click(function () {
        $("#country_slide").slideToggle(function() {
            if ($(this).is(":visible")) {
                alert("im visible!");
            }
        });
    });

    $('#close').click(function (e) {
        e.preventDefault();
        $('#country_slide').slideToggle();
    });

});

Then I want some AJAX code to load a html file into the div when the div is expanded. 然后,当扩展div时,我想要一些AJAX代码将html文件加载到div中。 The trick is that if the HTML is loaded successfully, I want it to avoid reloading the HTML file again if the div is closed and repoened, since I have already loaded it, and just simply toggle the content in and out with the button. 诀窍是,如果成功加载HTML,我希望它避免在div关闭并重新加载后避免重新加载HTML文件,因为我已经加载了它,只需简单地使用按钮切换内容即可。 The code I have for this (which I got help on from here is this): 我拥有的代码(从这里得到帮助的是这个):

http://jsfiddle.net/spadez/uhEgG/55/ http://jsfiddle.net/spadez/uhEgG/55/

$(function () {
    $('#country_link').on('click', function (e) {
        // Prevent from following the link, if there is some sort of error in
        // the code before 'return false' it would still follow the link.
        e.preventDefault();

        // Get $link because 'this' is something else in the ajax request.
        var $link = $(this);
        // Exit if the data is loaded already
        if ($link.data('loaded') === true)
            return false;

        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: '/ajax/test.html',
            timeout: 5000,
            beforeSend: function () {
            },
            success: function (data, textStatus) {
                $("#country_slide").html(data);
                alert('request successful');
                // If successful, bind 'loaded' in the data
                $link.data('loaded', true)
            },
            error: function (xhr, textStatus, errorThrown) {
                $("#country_slide").html('Error');
            },
            complete: function () {
            },
        });
    });
});

I haven't been able to get this working yet though. 我还没能使它正常工作。 So my question is, is it actually possible to do this, and if it is, can anyone with more experience with jquery please help me integrate the div toggle with the ajax loading script. 所以我的问题是,实际上是否有可能做到这一点,如果可以,那么对jquery有更多经验的人可以帮助我将div切换与ajax加载脚本集成在一起。

This is one of my first jquery scripts and I am having a bit of a hard time with it, perhaps it is not for beginners. 这是我的第一个jquery脚本之一,我在使用它时遇到了一些麻烦,也许不是针对初学者的。 Thank you. 谢谢。

I edited the fiddle you posted adding the call to slideToogle() where appropriate. 我编辑了您发布的小提琴 ,在适当的地方添加了对slideToogle()的调用。 Also added a div element to hold the loaded html code. 还添加了一个div元素来保存已加载的html代码。

<div id="country_slide">
    <a href="#" id="close">Close</a>
    <div class=".content"></div> <!-- This is the div I added -->
</div>

You can check the log messages in the console to verify that the code is doing what you expect. 您可以在控制台中检查日志消息,以验证代码是否按预期执行。 The URL for the Ajax call you were doing always returned an error so I changed to the URL that jsfiddle provides for testing: /echo/html/ . 您正在执行的Ajax调用的URL始终返回错误,因此我更改为jsfiddle提供用于测试的URL: /echo/html/

Here's the modified JS code: 这是修改后的JS代码:

$(function () {
    $('#close').click(function (e) {
        e.preventDefault();
        $('#country_slide').slideToggle();
    });

    $('#country_link').on('click', function (e) {
        e.preventDefault();
        var $link = $(this);
        // Exit if the data is loaded already
        if ($link.data('loaded') === true) {
            console.log('Not using Ajax.');
            $("#country_slide").slideToggle();
            return false;
        }

        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: '/echo/html/',
            timeout: 5000,
            beforeSend: function () {
                $("#country_slide .content").html('<p>Loading</p>')
            },
            success: function (data, textStatus) {
                console.log('Fecthed with Ajax.');
                $("#country_slide .content").html(data);
                $("#country_slide").slideToggle();
                // If successful, bind 'loaded' in the data
                $link.data('loaded', true)
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('request failed');
            },
            complete: function () {
            },
        });
    });
});

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

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