JavaScript 错误 : "null or not an object"

标签 javascript null undefined

我这里有个奇怪的问题: http://jackyon.com.s146246.gridserver.com/thehappymonk/

我正在使用 queryloader2 插件和 maximage2 插件。它在所有现代浏览器上运行良好,但在 IE8 或更低版本上显示 javascript 错误。即使IE9显示错误,也能正常运行。

js error: 'a.Slide[...].content.length' is null or not an object.

删除任一插件时,页面运行没有错误。

万分感谢你们!这个问题已经让我发疯了几天。请帮我!谢谢!!

最佳答案

错误似乎来自 jquery.maximage.min.js,完整版本在这里:https://github.com/akv2/MaxImage/blob/master/lib/js/jquery.maximage.js

寻找什么的一个线索是有一个“use strict”指令,这对于打算在不支持严格模式的浏览器中运行的代码来说不是一个好主意。似乎没有任何充分的理由使用它。我想这只是时髦。

有一些基于 UA 字符串的浏览器嗅探(不是一个好兆头):

if($.browser.msie){
  // Stop IE from continually trying to preload images that we already removed
  document.execCommand("Stop", false);
}

无论如何,错误似乎与第 226 行周围的代码有关:

for(var j in $.Slides) {

  // Determine content (if container or image)
  if($.Slides[j].content.length == 0){
    c = '<img src="' + $.Slides[j].url + '" />';

  } else {
    c = $.Slides[j].content;
  }
  ...
}

所以找哪里Slides[j].content被赋值,在第625行有:

  $.Slides = Utils.construct_slide_object();

construct_slide_object是:

construct_slide_object: function() {
  var obj = new Object(),
      arr = new Array(),
      temp = '';

  $self.children().each(function(i) {
    var $img = $(this).is('img') ? $(this).clone() : $(this).find('img').first().clone();

这只是一个很长的写作方式吗$('<img />')

    // reset obj
    obj = {};

那么为什么要将它初始化为 each 之外的对象呢?功能?

    // set attributes to obj
    obj.url = $img.attr('src');
    obj.title = $img.attr('title') != undefined ? $img.attr('title') : '';

这只是一个很长的写法:

    obj.title = $img.attr('title');

attr将始终返回一个字符串,并且 $img.attr('title') != undefined仅当它返回空字符串时才为真。

    obj.alt = $img.attr('alt') != undefined ? $img.attr('alt') : '';
    obj.theclass = $img.attr('class') != undefined ? $img.attr('class') : '';
    obj.styles = $img.attr('style') != undefined ? $img.attr('style') : '';
    obj.orig = $img.clone();
    obj.datahref = $img.attr('data-href') != undefined ? $img.attr('data-href') : '';
    obj.content = "";

    // Setup content for within container
    if ($(this).find('img').length > 0) {

这是这段短代码中第三次使用它。

      if($.BrowserTests.cssBackgroundSize) {
        $(this).find('img').first().remove();

还有第四个,$(this)使用了 7 次。

      }
        obj.content = $(this).html();
      }

      // Stop loading image so we can load them sequentiallyelse{
      $img[0].src = "";

      // Remove original object (only on nonIE. IE hangs if you remove an image during load)
      if ($.BrowserTests.cssBackgroundSize) {

这似乎是某种浏览器推断:如果 backgrounsize 测试返回 false,则一定是 IE。

        $(this).remove();
      }

      // attach obj to arr
      arr.push(obj);
  });

  if(config.debug) { 
    debug(' - Slide Object - ');
    debug(arr);
  }
  return arr;
},

穿上你的 Boot 。 :-)

关于JavaScript 错误 : "null or not an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151171/

相关文章:

javascript - 使用环回REST API时如何更改gulp-Angular项目的端口号

返回后Javascript对象为空

java - 代码块未被触及

jquery - Internet Explorer 7 中的 JSON 问题

javascript - javascript代码在浏览器上可见

php - JSON 中的根节点

javascript - 什么时候在 JavaScript 中使用 null 或 undefined?

javascript - 检查关联数组中是否存在键

javascript - Gif 动画不会在新页面上重新启动...?

r - 使用 rpy2 将 NULL 从 Python 转换为 R