猿问

jQuery的isNumeric疑惑

贴上jQuery的源码


    isNumeric: function( obj ) {


        // As of jQuery 3.0, isNumeric is limited to

        // strings and numbers (primitives or objects)

        // that can be coerced to finite numbers (gh-2662)

        var type = jQuery.type( obj );

        return ( type === "number" || type === "string" ) &&


            // parseFloat NaNs numeric-cast false positives ("")

            // ...but misinterprets leading-number strings, particularly hex literals ("0x...")

            // subtraction forces infinities to NaN

            !isNaN( obj - parseFloat( obj ) );

    },

为什么需要obj - parseFloat( obj )而不是直接obj - obj

是为了排除什么情况?


在问题补充回答。

空字符串的情况


Helenr
浏览 378回答 1
1回答

互换的青春

 // ...but misinterprets leading-number strings, particularly hex literals ("0x...")注释里已经写的很明白了啊
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答