Javascript变量冲突

时间:2011-10-21 16:15:26

标签: javascript internet-explorer

我在同一页面上有两个脚本,两个都运行滑块,第一个打破第二个,特别是悬停事件,仅在IE7和IE8中。我想知道两个脚本之间是否存在变量冲突。这是第一个:

<script type="text/javascript">
    $(function(){
        $('#slider')
            .anythingSlider({ 
                theme               : "minimalist-square",      
                expand              : false,                    
                resizeContents      : true,                     
                buildArrows         : true,                     
                buildNavigation     : true,                     
                toggleArrows        : true,                     
                autoPlay            : true                      
            });
    });
</script>

这是第二个脚本的一部分,可能导致错误?

<script type='text/javascript'>

    $(function() {
        slider = $('.artist-homepage-slider .artist-wrapper');
        handle = $('.homepage-slider .handle');
        //productwidth = 20;
        productwidth = 248;
        products = $('.artist-homepage-slider .product');
        productscount = products.length;
        images = products.find('img');
        productswidth = 0;
        .
                    .
                    .
                    . 

        function slideleft() {
            v = s.slider('option', 'value');
            if (v > 0)
                v -= 50;
            ui.value = v;
            s.slider('option', 'value', v);
            f = s.slider('option', 'slide');
            f(null,ui);
        }

        function slideright() {
            v = s.slider('option', 'value');
            if (v < fullWidth)
                v += 50;
            ui.value = v;
            s.slider('option', 'value', v);
            f = s.slider('option', 'slide');
            f(null,ui);
        }
    });

可能是导致问题的第二个脚本中的滑块声明吗?在其他文件中调用了其他脚本,其中一个可能导致问题吗?在IE7和IE8中滑块显示,只是滑动功能不起作用。 IE7和8以及导致此行为的其他浏览器之间的根本区别是什么?

1 个答案:

答案 0 :(得分:0)

我没有看到变量“s”在任何地方声明,但你在sliderRight和sliderLeft函数中都使用它。这适用于其他浏览器吗?

此外,您将大多数变量附加到全局范围,因为您没有使用“var”声明它们。为了缩小范围,从而减少冲突的可能性,使用var来声明局部变量