在函数中传入$(this)和addClass

时间:2017-01-07 19:13:36

标签: javascript jquery

我目前正在尝试将jQuery传递给函数,以便稍后我可以修改该元素。

$(document).on('pjax:click', function(event) {
    Loader.send($(this));
});

$(document).on('pjax:complete', function() {
    Loader.complete();
});

Loader对象

var Loader = {
    loading: false,
    lastPage: '',
    currentPage: '',
    target: null,

    send: function(_this)
    {
        this.lastPage = window.location.href;
        this.target = _this;
        this.load();
    },

    complete: function()
    {
        this.currentPage = window.location.href;

        // Handle the menu active states
        // Current (new page) url
        var cUrl = this.currentPage.replace('http://', '')
            .replace('https://', '')
            .replace('admin/', '')
            .split('/');

        // Previous page
        var pUrl = this.lastPage.replace('http://', '')
            .replace('https://', '')
            .replace('admin/', '')
            .split('/');

        // If new and old url's are different and they are different sections change the menu
        if(cUrl[1] !== pUrl[1] && this.lastPage !== this.currentPage)
        {
            $(".nav li").removeClass('active');

            this.target.addClass('active');

            console.log('Menu shit');
        }


        this.load();
    },

    load: function() {
        console.log(this);
        $("body").toggleClass('loading');
    }
};

我在使用this时尝试了$(this)Loader.send()但是我似乎无法在complete方法中添加一个类。

有没有办法通过Loader.send()发送并稍后修改它?

1 个答案:

答案 0 :(得分:0)

您正在尝试传递整个文档并在文档级别添加类。请提供完整的HTML,以便我们可以尝试回答它。

相关问题