jQuery之$工具方法
阿新 • • 發佈:2020-07-13
方法
$.each():遍歷陣列或物件中的資料。
$.trim():去除字串兩邊的空格。
$.type(obj):得到資料的型別。
$.isArray(obj):判斷是否是陣列。
$.isFunction(obj):判斷是否是函式。
$.parseJSON(json):解析json字串轉換為js物件/陣列。
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>$工具方法</title> </head> <body> <script type="text/javascript" src="../js/jquery.min.js"></script> <script type="text/javascript"> $(function (){ //1.$.each():遍歷陣列或物件中的資料 var person = { name: 'tom', age: 12 }; $.each(person, function (key, value) { console.log(key, value); });//2.$.trim():去除字串兩邊的空格 var str = ' hello world '; console.log($.trim(str)); //3.$.type(obj):得到資料的型別 console.log($.type($) === "function"); //4.$.isArray(obj):判斷是否是陣列 console.log($.isArray($()));//false console.log($.isArray([]));//true //5.$.isFunction(obj):判斷是否是函式console.log($.isFunction($));//true console.log($.isFunction($()));//false //6.$.parseJSON(json):解析json字串轉換為js物件/陣列 /* * json整體就2種類型: * 1.json物件:{key1:value1,key2:value2} * 2.json陣列:[value1,value2] * 3.key只能是字串 * 4.value的型別: * number * string * boolean * null * [] * {} */ var json = '{"username":"jack", "age" : 12}'; console.log($.parseJSON(json));//js物件 json = '[{"username":"jack", "age" : 12},{"username":"Tom", "age" : 13}]'; console.log($.parseJSON(json));//js陣列 }); </script> </body> </html>