首页 > Web开发 > 详细

JS中的各种检测

时间:2014-01-26 15:26:49      阅读:394      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 //null 只在肯定返回null值时才使用null比较
 2 var element = document.getElementById("my-div");
 3 if (element === null) {
 4 
 5 };
 6 //string number boolean undefined
 7 var num = 123;
 8 if (typeof num === "number") {
 9 
10 };
11 
12 /*
13 检查引用值
14 Date RegExp Error
15 跨域的检查会有问题
16 */
17 if (value instanceof Date) {
18 
19 };
20 
21 //检查函数
22 if (typeof myFunc === "function") {};
23 //if (myFunct instanceof Function) {}; 不能跨域
24 //浏览器函数 因为IE9之前返回有问题
25 if ("querySelectorAll" in document) {};
26 
27 //检查数组
28 function isArray(value){
29     if (typeof Array.isArray === function) {
30         return Array.isArray(value);
31     }else{
32         return Object.prototype.toString.call(value) === "[object Array]"; //IE9以下
33     }
34 }
35 
36 //检查属性
37 if ("related" in object) {};
38 if (object.hasOwnProperty("related")) {}; //仅检查实例对象
bubuko.com,布布扣

JS中的各种检测

原文:http://www.cnblogs.com/goodspeed/p/3533806.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!