jquery判断节点存在

var id = document.getElementId("id");

if(typeof(id) == "undefined" || id == null) {

  console.log("节点不存在");

else{

  console.log("节点存在");

}

注意:typeof(id) == "undefined"  而不是 typeof(id) == undefined

一般typeof返回的数据类型:

  "undefined"

  "string"

  "boolean"

  "number"

  "object"

  "function"

1、number

  console.log(typeof(123)); 

  console.log(typeof(NaN));

  console.log(typeof(0));

2、string

  console.log(typeof('a'));

3、boolean

  console.log(typeof(true));

  console.log(typeof(false));

4、undefined

  变量没有对应对象或变量的值为undefined

  console.log(typeof(a));

  var a = undefined;console.log(typeof(a));

5、object

  var a = {}; console.log(typeof(a));//对象

  var a = []; console.log(typeof(a));//数组

        console.log(typeof(null));

6、function

  var a = function(){}; console.log(typeof(a));

//Object.prototype.toString.apply(obj)

猜你喜欢

转载自www.cnblogs.com/zj68/p/12897730.html