为什么element.style.left返回NaN? [英] Why is element.style.left returning NaN?

查看:147
本文介绍了为什么element.style.left返回NaN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Internet Explorer 7中遇到了一个奇怪的错误,当我在浮点数上调用Math.round时,它给出了一个无效的参数错误。请考虑以下内容:

I'm getting a weird error where in Internet Explorer 7, when I call Math.round on a float it gives me an "Invalid Argument" error. Consider the following:

var elementLeft = parseInt(element.style.left); // Here we're actually getting NaN
function Foo(x) {
  this.x = x;

  this.apply = function(element) {
    element.style.left = Math.round(this.x) + 'px';
  };
}
Foo(elementLeft);

在这种情况下 x 负数和元素只是我的页面中的一个DOM元素(实际上是一个div)。

In this case x is a non-negative number and element is just a DOM element in my page (a div, in fact).

任何想法

编辑: x 参数传入的变量实际上已经初始化作为 parseInt(element.style.left)。看来,我第一次尝试阅读 element.style.left ,IE实际上是回复NaN。我已经更新了代码以反映这一点。任何人都知道任何解决方法吗?

The variable passed in as the x parameter is actually initialized earlier as parseInt(element.style.left). It appears that the first time I try to read element.style.left, IE is actually giving back NaN. I have updated the code to reflect this. Anyone know any workarounds for this?

推荐答案


看来,我第一次尝试读取元素.style.left,IE实际上是回复NaN。

It appears that the first time I try to read element.style.left, IE is actually giving back NaN.

第一次读取 element.style。离开,元素上是否有任何 left 样式设置?记住 element.style 仅反映在内联 style =...属性中设置的样式属性,而不是应用

The first time you read element.style.left, is there actually any left style set on the element? Remember element.style only reflects style properties set in the inline style="..." attribute and not those applied by stylesheets.

如果您没有设置内联样式, style.left 将给您 undefined 对象,确实 parseInt 到NaN。

If you haven't set an inline style, style.left will give you the undefined object, which does indeed parseInt to NaN.

这篇关于为什么element.style.left返回NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆