2015-12-17 29 views
1

我遇到了算術問題並轉換爲數字。我認爲。將問題轉換爲數字

我需要得到一個元素的總寬度。在這種情況下,它是一個按鈕,我要通過2.

var $buttonWidth = parseInt($(this).outerWidth(true)); 
    console.log($.isNumeric($buttonWidth)); 
    console.log(isNaN($buttonWidth)); 

    console.log($buttonWidth); 
    $buttonWidth/2; 

    console.log($buttonWidth); 

第一日誌被返回true來劃分它,二是返回false,第三被返回元件和第四的總寬度日誌也返回日誌的總寬度。我做錯了什麼?

+0

你有什麼產出預期? – hindmost

+0

我期待第四次日誌成爲三分之二的分數。 – Tgrosk

+0

'$ buttonWidth = $ buttonWidth/2;'你不會將結果重新分配給變量。 width/2只是一個表達式,它不會改變原來的變量。 – Shilly

回答

5

您必須將新值分配給$ buttonWidth。

$buttonWidth = $buttonWidth/2; 

$buttonWidth /= 2;