【问题标题】:Script error, where to add if脚本错误,在哪里添加 if
【发布时间】:2013-07-18 21:36:45
【问题描述】:

我有这个脚本,但在 IE8 中我得到错误:'photos are undiefienied',如果要解决这个问题,应该在哪里做:

    /*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
window.addEvent('load', function() {

    jQuery("#slideimg1").css({
            "background-image" : "url("+url+"/" + photos[0].image + ")"
    });
    if(photos.length >= 2){
        jQuery("#slideimg2").css({
                "background-image" : "url("+url+"/" + photos[1].image + ")"
        });
    }


    // Backwards navigation
    jQuery("#cp-back").click(function() {
        //stopAnimation();
        navigate("back");
    });
    // Forward navigation
    jQuery("#cp-next").click(function() {
        //stopAnimation();
        navigate("next");
    });


    //jQuery("#preload").hide();
    var activeContainer = 1;    
    var currentImg = 0;
    var animating = false;
    var first = false;
    var navigate = function(direction) {
        // Check if no animation is running. If it is, prevent the action
        if(animating) {
            return;
        }
        // Check which current image we need to show
        if(direction == "next") {
            currentImg++;
            if(currentImg == photos.length + 1) {
                currentImg = 1;
            }
        } else {
            currentImg--;
            if(currentImg == 0) {
                currentImg = photos.length;
            }
        }

        // Check which container we need to use
        var currentContainer = activeContainer;
        if(activeContainer == 1) {
            activeContainer = 2;
        } else {
            activeContainer = 1;
        }

        showImage(photos[currentImg - 1], currentContainer, activeContainer);

    };

    var currentZindex = -1;
    var showImage = function(photoObject, currentContainer, activeContainer) {
        //alert(currentContainer);
        animating = true;
        // Make sure the new container is always on the background
        currentZindex--;
        if(!first){
          //alert(first);
            //alert("aa");
            // Set the background image of the new active container
            jQuery("#slideimg" + activeContainer).css({
                "background-image" : "url("+url+"/" + photoObject.image + ")"
                //"display" : "block",
                //"z-index" : currentZindex
            });
            // Fade out the current container
            // and display the header text when animation is complete
            jQuery("#slideimg" + currentContainer).fadeOut(effectTime,function() {
                animating = false;
            });
            jQuery("#slideimg" + activeContainer).fadeIn(effectTime);

            //first = false;
        }else{
            //alert("bbb");
            jQuery("#slideimg" + activeContainer).fadeOut(effectTime,function() {
                animating = false;
            });
            jQuery("#slideimg" + currentContainer).fadeIn(effectTime);
            first = false;
            //animating = false;
        }
    };

    var stopAnimation = function() {
        // Clear the interval
        clearInterval(interval);
    };

    // We should statically set the first image
    navigate("next");

    if(photos.length > 1){
        // Start playing the animation
        interval = setInterval(function() {
            navigate("next");
        }, slideshowSpeed);
    }

});

【问题讨论】:

  • 其他浏览器没问题???
  • 你没有在你的代码中定义photos
  • 要求代码的问题必须表明对正在解决的问题的最低理解。 包括尝试过的解决方案,为什么它们不起作用 ,以及预期的结果。 How to use a browser's console
  • 在 Firefox 和 chrome 中都可以,ie9 和 ie10 也可以,在 ie8 上我在主页上也出现错误“第 0 行”,我无法调试此错误,因为我在这里问...跨度>
  • @Jeff Noel - 在我在这里发布这个问题之前,我在更多的浏览器上测试了这个,包括 consols、chrome、firefox,即他的模式......这个问题出现在 ie8 和模式“标准”上"

标签: javascript jquery html internet-explorer-8 fatal-error


【解决方案1】:

我注意到您对“照片”的使用,但我没有看到在任何地方声明的变量... 至少,在 main 函数中(因为它不是全局的),声明你的 photos var:

var photos = [];

此外,就像 Chris 所说,始终检查 var 是否已定义。 :) 来源:https://stackoverflow.com/a/17748905/2599797

if (photos) // Simple
if (typeof(photos) != 'undefined') // Type matching, if photos defaults to true it won't pass
if ($.isArray(photos)) // jQuery based Javascript/Prototype native array check

【讨论】:

  • 如果照片未声明,那么您的第一个和第三个if 将失败。您必须使用if(window.photos) 或第二个if 语句,否则您将收到未捕获的引用错误。 Broken - 因为照片未声明。 Fixed - 因为我们添加了对未声明变量的适当检查。
  • 我用这个,这行得通(if (typeof(photos) != 'undefined')),谢谢
  • @BrandonBoone 自然而然,这就是为什么我说另外,因为我用var photos = []; 声明了它。 :)
猜你喜欢
  • 2018-10-13
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
相关资源
最近更新 更多