我知道我可能做错了什么。谁能指出为什么我会把top作为对象??
$(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)。是我的错。接受第一个ans。+1 to all用于快速回答。
发布于 2012-08-23 22:13:26
因为它本质上是window.top,也就是Window object。请改用var top,以防止将局部变量与全局变量(= window对象的属性)混合。
事实上,让var-ing你的函数变量成为一个常见的例程--以防止将来出现类似的问题。)
发布于 2012-08-23 22:14:02
您不需要使用substr来删除px。parseInt将为您完成以下操作:
topwithpx='0px';
var top = parseInt(topwithpx);
alert(top); //alerts "0"http://jsfiddle.net/kjMs9/3/
发布于 2012-08-23 22:13:46
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);
});https://stackoverflow.com/questions/12093615
复制相似问题