简体   繁体   English

在单独的回调中访问js变量(函数)

[英]Accessing js variable inside a separate callback (function)

I'm using a jquery script called magnific popup and I'm trying to access a variable I created in a callback function, inside another callback function but I can't work out how to do it. 我正在使用一个名为magnific popup的jquery脚本,并且试图访问在另一个回调函数内部的一个回调函数中创建的变量,但是我不知道该怎么做。 My code for the magnific init looks like this: 我的宏初始化代码如下所示:

$('.packery').magnificPopup({
    delegate: '.js-modal',
    type: 'ajax',
    mainClass: 'mfp-zoom-in',
    removalDelay: 500, //delay removal by X to allow out-animation
    callbacks: {
        elementParse: function(item) {
            item.community = item.el.attr('data-community');
            var communityClass = item.community;
            console.log(item.community);
            // this ^ does actually print the data-community 
            console.log('Parsing content. Item object that is being parsed:', item);
        },
        resize: function() {
            console.log('Popup resized');
            // resize event triggers only when height is changed or layout forced

            $('.mfp-bg').addClass(communityClass);
        }
    }
});

If I try and set $('.mfp-bg').addClass(communityClass); 如果我尝试设置$('.mfp-bg').addClass(communityClass); or $('.mfp-bg').addClass(item.community); $('.mfp-bg').addClass(item.community); I get a Uncaught ReferenceError: communityClass is not defined. 我收到了Uncaught ReferenceError:未定义communityClass。

I can't apply a class to mfp-bg inside elementParse as that element hasn't been created yet. 我不能将一个类应用于elementParse中的mfp-bg,因为该元素尚未创建。

I know that I can't use variables from different functions in javascript, but I'm a bit stuck at this point on how I can actually use the data-community attribute inside the resize callback, because it seems like I can only create the variable inside the elementParse callback? 我知道我无法在javascript中使用来自不同函数的变量,但此时我在如何实际使用resize回调中的data-community属性方面有些困惑,因为似乎我只能创建elementParse回调内的变量?

Any help would be much appreciated, cheers. 任何帮助将不胜感激,欢呼。

You could create a global variable, outside the function and assign item.community to it. 您可以在函数外部创建全局变量,然后为其分配item.community That way you will be able to access it in the other callback aswell 这样,您也可以在其他回调中访问它

For example: 例如:

var communityClass;
$('.packery').magnificPopup({
delegate: '.js-modal',
type: 'ajax',
mainClass: 'mfp-zoom-in',
removalDelay: 500, //delay removal by X to allow out-animation
callbacks: {
    elementParse: function(item) {
        item.community = item.el.attr('data-community');
        communityClass = item.community;
        console.log(item.community);
        // this ^ does actually print the data-community 
        console.log('Parsing content. Item object that is being parsed:', item);
    },
    resize: function() {
        console.log('Popup resized');
        // resize event triggers only when height is changed or layout forced

        $('.mfp-bg').addClass(communityClass);
    }
}
});

I realised after console.logging this there was already a currItem bit I could access, so I changed the code to this and it works fine now. 在console.logging之后,我意识到我已经可以访问一个currItem位,因此我将代码更改为此,现在可以正常工作了。

$('.packery').magnificPopup({
    delegate: '.js-modal',
    type: 'ajax',
    mainClass: 'mfp-zoom-in',
    removalDelay: 500, //delay removal by X to allow out-animation
    callbacks: {
        elementParse: function(item) {
            item.community = item.el.attr('data-community');
        },
        resize: function() {
            $('.mfp-bg').addClass(this.currItem.community);
        }
    }
});

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

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