简体   繁体   English

IE8的JavaScript无效参数错误

[英]JavaScript invalid argument error with IE8

IE8 is giving me an invalid argument error at character 9 of the last statement in this loop (Character 9 is the first letter of the icon variable.): IE8在此循环中最后一条语句的字符9处给我一个invalid argument错误(字符9是icon变量的第一个字母。):

var JOB_STATUS_ICON_DISTANCE = 125;
var JOB_STATUS_ICON = {};
var center = parseInt($('#job-status-window').css('width')) / 2;
var jobStatusIcons = ['A', 'B', 'C', 'D', 'E'];

for (var i=0; i<jobStatusIcons.length; i++) {
    var name = jobStatusIcons[i];
    var icon = document.getElementById('job-status-' + name);
    var width = icon.naturalWidth;
    var centerPoint = (center-(width/2));

    JOB_STATUS_ICON[name+'Center'] = centerPoint;
    JOB_STATUS_ICON[name+'Left']   = centerPoint - JOB_STATUS_ICON_DISTANCE;
    JOB_STATUS_ICON[name+'Right']  = centerPoint + JOB_STATUS_ICON_DISTANCE;
    JOB_STATUS_ICON[name] = $('#job-status-'+name);

    icon.style.left = JOB_STATUS_ICON[name+'Right'] + 'px';  // error here...
}

Can anyone tell me why? 谁能告诉我为什么? This works fine in modern browsers. 这在现代浏览器中可以正常工作。

Just for some context, this is for monitoring the status of a job in progress (5 steps). 仅在某些情况下,这是为了监视进行中的作业的状态(5个步骤)。 I'm keeping track of the positions of 5 images in JOB_STATUS_ICONS. 我正在跟踪5张图片在JOB_STATUS_ICONS中的位置。 Each image slides in from the right to the center position, stays there while that part of the job is processing, then slides out to the left. 每个图像从右侧滑入到中心位置,在作业的那一部分正在处理时停留在该位置,然后向左滑出。 The code above is just some optimization for establishing the left, center and right position of each image (they're all different widths). 上面的代码只是一些优化,用于建立每个图像的左,中和右位置(它们都是不同的宽度)。

The last line in the loop, which causes the problem, is setting the initial position of the image. 循环中导致问题的最后一行是设置图像的初始位置。

This is probably being caused by the fact that IE8 doesn't support the naturalWidth property from which your centerPoint is being calculated. 这可能是由于IE8不支持从其中计算centerPointnaturalWidth属性引起的。

var width = icon.naturalWidth;
var centerPoint = (center-(width/2));

Here's a short piece of Javascript written by Jack Moore that simulates naturalWidth in IElt8 这是杰克·摩尔(Jack Moore)编写的一小段Javascript,可在IElt8中模拟naturalWidth

function getNatural (DOMelement) {
    var img = new Image();
    img.src = DOMelement.src;
    return {width: img.width, height: img.height};
  }

Source: http://www.jacklmoore.com/notes/naturalwidth-and-naturalheight-in-ie/ 资料来源: http : //www.jacklmoore.com/notes/naturalwidth-and-naturalheight-in-ie/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM