在jQuery中使用全局变量

时间:2012-02-25 09:34:05

标签: jquery global-variables

我在页面上使用了2个jQuery类

1检查下拉菜单中的选项并根据选择更改主图像 2是主图像的悬停效果

我想要做的是将更改从1传递到2

我已经定义了类之外的全局变量,并确保我不会在任何一个类中重新定义它们

下面是更改图像的函数(variation_image),我现在要做的就是使用其他类中的variation_image

TIA

    function show_variation(variation) {
    var img = $('div.images img:eq(0)');
    var link = $('div.images a.zoom:eq(0)');
    o_src = $(img).attr('original-src');
    var o_link = $(link).attr('original-href');

    variation_image = variation.image_src;
    var variation_link = variation.image_link;

    $('.variations_button').show();
    $('.single_variation').html( variation.price_html + variation.availability_html );

    if (!o_src) {
        $(img).attr('original-src', $(img).attr('src'));
    }

    if (!o_link) {
        $(link).attr('original-href', $(link).attr('href'));
    }

    if (variation_image && variation_image.length > 1) {    
        $(img).attr('src', variation_image);
        $(link).attr('href', variation_link);
    } else {
        $(img).attr('src', o_src);
        $(link).attr('href', o_link);
    }

    if (variation.sku) {
         $('.product_meta').find('.sku').text( variation.sku );
    } else {
         $('.product_meta').find('.sku').text('');
    }

    $('.single_variation_wrap').slideDown('200').trigger('variationWrapShown').trigger('show_variation'); // depreciated variationWrapShown
}
############编辑

// FILE ONE

var variation_image = "";

jQuery(document).ready(function($) {

// LOTS OF OTHER CODE HERE

function show_variation(variation) {
    var img = $('div.images img:eq(0)');
    var link = $('div.images a.zoom:eq(0)');
    o_src = $(img).attr('original-src');
    var o_link = $(link).attr('original-href');

    variation_image = variation.image_src;
    var variation_link = variation.image_link;

    $('.variations_button').show();
    $('.single_variation').html( variation.price_html + variation.availability_html );

    if (!o_src) {
        $(img).attr('original-src', $(img).attr('src'));
    }

    if (!o_link) {
        $(link).attr('original-href', $(link).attr('href'));
    }

    if (variation_image && variation_image.length > 1) {    
        $(img).attr('src', variation_image);
        $(link).attr('href', variation_link);
    } else {
        $(img).attr('src', o_src);
        $(link).attr('href', o_link);
    }

    if (variation.sku) {
         $('.product_meta').find('.sku').text( variation.sku );
    } else {
         $('.product_meta').find('.sku').text('');
    }

    $('.single_variation_wrap').slideDown('200').trigger('variationWrapShown').trigger('show_variation'); // depreciated variationWrapShown
}

// LOTS OF OTHER CODE HERE

});

//在上面的文件之后加载的单独文件

var $j = jQuery.noConflict();



$j(function(){


$j(document).ready(function () {
    $j('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();

    console.log('variation_image ' + variation_image);
});

// LOTS OF OTHER CODE HERE TO SWAP IMAGES

});

0 个答案:

没有答案