IE8 的 JavaScript 无效参数错误

标签 javascript html css internet-explorer-8

IE8 在此循环中最后一条语句的字符 9 处给我一个无效参数错误(字符 9 是图标变量的第一个字母。):

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...
}

谁能告诉我为什么?这在现代浏览器中运行良好。

仅在某些情况下,这是为了监视正在进行的作业的状态(5 个步骤)。我正在跟踪 JOB_STATUS_ICONS 中 5 个图像的位置。每个图像从右侧滑入中心位置,在处理该部分作业时停留在那里,然后滑出到左侧。上面的代码只是对每个图像的左、中、右位置进行了一些优化(它们都是不同的宽度)。

导致问题的循环中的最后一行是设置图像的初始位置。

最佳答案

这可能是因为 IE8 不支持计算 centerPointnaturalWidth 属性。

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

这是一段由 Jack Moore 编写的模拟 IElt8 中的 naturalWidth 的 Javascript

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

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

关于IE8 的 JavaScript 无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612971/

相关文章:

iphone - 让网站用户知道有可用的 iOS 应用程序

javascript - 如何在输入文本框AngularJS中默认显示鼠标光标位置词尾?

jquery - 防止 jQuery 在元素鼠标悬停时将文档顶部推到顶部?

html - 验证密码和用户名nodejs mysql

html - 在 Internet Explorer 中,输入字段比选择框长几个像素;我怎样才能使它们的长度相同?

javascript - HTML - 如何使这些 div 保持在一行?

javascript - 自引用对象在 JavaScript 函数中如何工作?

php - 数组到javascript

javascript - 2 位数字的正则表达式

html - 需要删除 Body 中元素周围的填充