【问题标题】:History api statechange function script duplicates function calls each time change occurs每次发生更改时,历史 api statechange 函数脚本都会重复函数调用
【发布时间】:2013-09-05 10:37:29
【问题描述】:

我在一个简单的演示中使用 jquery 历史插件,每次单击导航项时都会加载到特定页面的一部分中,但我发现每次单击新链接时,@987654322 @代码重复。每次代码在此函数中完成时是否可以取消绑定,或者任何人都可以建议我可能在哪里出错?

我的测试页http://kylehouston.com/_testing/history/page1.html

JS

navLink.on('click', function(e) {

        e.preventDefault();

        var el = $(this);

        //get the href and remove begining /
        url = el.attr('href')
            .slice(1);

        //do a check to see if this link is already active
        if (el.hasClass('is-active')) {
            return false;
        }

        navLink.removeClass('is-active');
        el.addClass('is-active');


        History.pushState(null, null, url);


        // Bind to StateChange Event
        History.Adapter.bind(window, 'statechange', function() {
            var State = History.getState();

            mainContent.fadeOut(function() {
                $(this)
                    .empty();

                console.log('this ran');

                preloader.fadeIn();

                loadContent(mainContent, location.pathname);
            });

            History.Adapter.unbind();
        });


        //empty mainContent then empty it
        mainContent.fadeOut(function() {
            $(this)
                .empty();

            //display preloader
            preloader.fadeIn();

            //check if content already exists in pages{} if not then make call then save to pages{} or retrieve from pages{} if already exists
            loadContent(mainContent, url);
        });
    });

【问题讨论】:

    标签: javascript jquery history.js


    【解决方案1】:

    你有没有尝试过移动

    // Bind to StateChange Event
    History.Adapter.bind(window, 'statechange', function() {
        var State = History.getState();
    
        mainContent.fadeOut(function() {
            $(this).empty();
            console.log('this ran');
            preloader.fadeIn();
            loadContent(mainContent, location.pathname);
        });           
    });
    

    之前

    navLink.on('click', function(e){
    

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2015-12-17
      • 1970-01-01
      • 2018-03-23
      • 2017-05-30
      • 2021-07-19
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      相关资源
      最近更新 更多