Javascript变量冲突

Javascript Variable Conflict

本文关键字:冲突 变量 Javascript      更新时间:2023-09-26

我在同一个页面上有两个脚本,都运行滑块,第一个打破了第二个,特别是悬停事件,仅在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和ie8以及其他会导致这种行为的浏览器之间的根本区别是什么?

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

你也有你的大多数变量附加到全局作用域,因为你没有声明他们与"var"。为了缩小作用域,从而减少冲突的机会,使用var来声明你的局部变量