Agri-Net 1558 Agri-Net

來源:互聯網
上載者:User

標籤:style   color   get   strong   int   string   

JQuery工具方法.(1)$.isNumeric(obj)    此方法判斷傳入的對象是否是一個數字或者可以轉換為數字.    isNumeric:  function( obj ) {        // parseFloat NaNs numeric-cast false positives (null|true|false|"")       // ...but misinterprets leading-number strings, particularly hex literals ("0x...")       // subtraction forces infinities to NaN        return obj - parseFloat( obj ) >= 0;  },用‘-‘號先將obj轉換為數字,這麼做是因為如果傳入的字串中含有非數字字元,那麼obj將被轉換為NaN,isNumeric將返回false.alert( ‘55a‘-0) //NaN注意:js的原生方法parseFloat在處理16進位字串時會出現轉換錯誤,他會取16進位的標誌0x的0..... var f = parseFloat(‘0xAAAA‘ , 2);alert(f); //0
Test_Script var obj = {toString:  function(){      return ‘6‘ ;}}; var f = $.isNumeric(obj);alert(f);//true
(2)isPlainObject(obj)此方法判斷傳入的對象是否是‘純淨‘的對象,即使用字面量{},或new Object()建立的對象.Test_Script function Person(name, age) {     this .name = name;     this .age = age;};                var p = $.isPlainObject(  new Person());              alert(p); //false              alert($.isPlainObject({})); //true              alert($.isPlainObject(document.getElementById( ‘div1‘))); //false
isPlainObject(obj)源碼isPlainObject:  function( obj ) {               //不是object或者是DOM元素或是window返回false                if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {                       return  false ;              }
               // Support:  Firefox <20               // The try/catch suppresses exceptions thrown when attempting to access               // the "constructor" property of certain host objects,  ie. |window.location|               // https://bugzilla.mozilla.org/show_bug.cgi?id=814622                try {                      //是自訂對象返回false                      //判斷自訂對象的依據是如果isPrototypeOf方法是從{}的原型對象中繼承來的那麼便是自訂對象                       if ( obj.constructor &&                                  !hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {                             return  false ;                     }              }  catch ( e ) {                       return  false ;              }
               // If the function hasn‘t returned already, we‘re confident that               // | obj | is a plain object, created by {} or constructed with new Object                return  true ;       },

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.