4

我对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 
4

2 回答 2

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);
}); 
于 2013-01-24T13:43:58.197 回答
1

只需在这条线上进行简单的更正。然后你会得到想要的输出..

var center = parseInt(position.left) - (parseInt(width) / 2);
于 2013-01-24T13:46:43.200 回答