JS parseInt() issue
JS parseInt() issue
我知道我可能做错了什么。有人能指出我为什么会成为目标吗??
$(document).ready(function(){
topwithpx='0px';
alert(topwithpx);
topstr=topwithpx.substr(0,topwithpx.length-2);
alert(topstr);
top=parseInt(topstr);
alert(top);
});
http://jsfiddle.net/kjMs9/
谢谢大家:"top"是保留关键字(Window.top)。我的错。接受第一次应答。+1对所有人进行快速应答。
因为它本质上是window.top
,它是Window对象。请改用var top
,以防止局部变量与全局(=window
对象的属性)变量混合。
事实上,让var
——使用函数变量成为一种常见的例程——以防止将来出现类似的gotcha。)
您不需要使用substr
来删除px
。parseInt
将为您做到这一点:
topwithpx='0px';
var top = parseInt(topwithpx);
alert(top); //alerts "0"
http://jsfiddle.net/kjMs9/3/
window.top
是DOM 0的一部分,不能分配数字。
避免使用全局变量。用var
确定范围
$(document).ready(function(){
var topwithpx, topstr, top;
topwithpx='0px';
alert(topwithpx);
topstr=topwithpx.substr(0,topwithpx.length-2);
alert(topstr);
top=parseInt(topstr);
alert(top);
});
top
是window
对象(MDN)的默认属性。用其他名称命名变量。
top
是window
的只读属性(至少对Mozilla是这样,但可能对所有其他大型浏览器也是如此)。
只需将top
更改为类似topInt
的其他内容即可。此外,使用var
声明变量(例如var topInt = parseInt(...)
。如果不使用var
,则默认使用window
属性,因此为只读行为。
顺便说一下,使用console.log
而不是alert
会更好
top
是javascript window
属性。你可以使用顶部作为一个变量通过做这个
var top = ...
$(document).ready(function(){
topwithpx='0px';
alert(topwithpx);
topstr=topwithpx.substr(0,topwithpx.length-2);
alert(topstr);
var top=parseInt(topstr);
alert(top);
});
您缺少变量的delcaration
相关文章:
- issue with FB.Event.subscribe
- 关于 Javascript 中 parseInt() 的说明
- 使用Jquery Issue垂直重新排序Divs
- 为什么这是真的:parseInt(1111111111111,2)===parseInt(111111111111,2)
- jquery中出现NaN错误.已经尝试过ParseInt,但仍然不起作用
- parseInt("08") returns 0
- parseInt()方法如何返回整数
- 创建了一个选项卡,但点击时不会起作用-jQuery issue/newbie
- parseInt的行为很奇怪——可能吧
- JS parseInt带逗号的字符串
- jQuery find() Issue
- $route.reload(); Issue
- PHP json_encode issue
- 数组上的ParseInt()方法
- Three.js issue creating meshes outside of loader's load(
- Jquery :gt(value) issue
- pickaday maxDate issue
- 为什么parseInt()比Firefox中的*1慢得多
- 为什么不'将字符串转换为int不正确(parseInt)
- JS parseInt() issue