小计算问题

时间:2013-01-24 13:43:03

标签: javascript jquery

我的javascript计算有一个小问题。我有这样的功能:

$( "#main-nav li[class]" ).each(function( index ) {
     var position = $(this).offset();
     var width = $(this).width();
     var center = parseInt(position) - (parseInt(width) / 2);
     console.log("Position: " + position.left + ", width: " + width +  ", center: " + center);
});   

但它导致了这一点。任何人都知道如何不进行计算?

Position: 722, width: 83, center: NaN 

2 个答案:

答案 0 :(得分:7)

position.left

$( "#main-nav li[class]" ).each(function( index ) {
     var position = $(this).offset();
     var width = $(this).width();
     var center = parseInt(position.left) - (parseInt(width) / 2);
     console.log("Position: " + position.left + ", width: " + width +  ", center: " + center);
}); 

答案 1 :(得分:1)

只需对此行进行简单修正即可。然后你会得到所需的输出..

var center = parseInt(position.left) - (parseInt(width) / 2);