【问题标题】:Ajax Loader showing twice in functionAjax Loader 在函数中显示两次
【发布时间】:2017-09-13 06:48:41
【问题描述】:

我在这里创建了一个小提琴...https://jsfiddle.net/qukhn4uk/

如您所见,我有一个用于单击网格图像的对象,该对象打开一个弹出窗口并通过 ajax 加载该人的数据(您显然不会在小提琴中看到数据加载,但您明白了)。这里一切正常。处理这个的对象是:

Stories = {
    flyout: '#flyout',
    closeFlyout: '#flyout .close-flyout',
    storyTrigger: '.story .story-trigger',
    ajaxContentContainer: '#flyout .content',
    loader: '<i class="fa fa-spin fa-spinner"></i>',
    body: 'body',
    init: function() {
        $(this.storyTrigger).click(this.showStory.bind(this));
        $.ajaxSetup({cache:false});
        $(document).on("click", this.closeFlyout, this.closeStory.bind(this));
    },
    showStory: function(e) {
        e.preventDefault();
        var target = $(e.target),
            targetName = target.data("name");
        $(this.flyout).css("transform", "translateX(-100%)");
        $(this.body).css("overflow", "hidden");
        $(this.ajaxContentContainer).append(this.loader);
        $(this.ajaxContentContainer).load("/story/" + targetName);
    },
    closeStory: function() {
        $(this.flyout).css("transform", "translateX(100%)");
        $(this.ajaxContentContainer).empty();
        $(this.body).css("overflow", "auto");
    }
}

然后我有另一个加载函数,用于打开弹出窗口并根据 url 中的哈希加载数据。这是处理那个的对象...

DirectStory = {
        storyDiv: '.story',
        init: function() {
            var self = this;
            if ( window.location.hash != '' ) {
                $(this.loadStory.bind(this));
            }
            $.ajaxSetup({cache:false});
        },
        loadStory: function() {
            var hash = window.location.hash,
                story = hash.substring(1);
            targetStory = $(this.storyDiv).find("[data-name='" + story + "']");
            targetStory.click();
        }
    }

一切都很好,但有一个小故障。由于某种原因,DirectStory 对象导致 Stories 对象中的 ajaxLoader 加载两次。有人可以帮我弄清楚为什么会这样吗?谢谢!

更新:我发现 targetStory.click() 在 DirectStory 对象内运行了两次。我曾尝试先解除绑定,但这无济于事。为什么会运行两次?

【问题讨论】:

    标签: javascript jquery ajax hash bind


    【解决方案1】:

    我已经为任何登陆这里的人解决了这个问题......

    targetStory 变量以我存储它的方式找到了两个触发器。

    我只是将 targetStory 变量更新为...

    targetStory = $(".full-link[data-name='" + story + "']");

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多