js、dom基础知识

1. 事件 onclick onmouseover等

<button onclick="displayDate()">现在的时间是?</button>

  function displayDate(){alert("date");}

2. 类型

typeof "John"                 // 返回 string 
typeof 3.14                   // 返回 number
typeof NaN                    // 返回 number
typeof false                  // 返回 boolean
typeof [1,2,3,4]              // 返回 object
typeof {name:'John', age:34}  // 返回 object
typeof new Date()             // 返回 object
typeof function () {}         // 返回 function
typeof myCar                  // 返回 undefined (如果 myCar 没有声明)
typeof null                   // 返回 object

3. 如何看一个类型是不是数组,可以通过构造函数来判断。

"John".constructor                 // 返回函数 String()  { [native code] }
(3.14).constructor                 // 返回函数 Number()  { [native code] }
false.constructor                  // 返回函数 Boolean() { [native code] }
[1,2,3,4].constructor              // 返回函数 Array()   { [native code] }
{name:'John', age:34}.constructor  // 返回函数 Object()  { [native code] }
new Date().constructor             // 返回函数 Date()    { [native code] }
function () {}.constructor         // 返回函数 Function(){ [native code] }

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);
function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;
}

4.类型转换

 

//转为字符串
let x=55;
var t = x.toString();
var t = String(x);
var t = ""+x;

//字符串转为数字
x = parseInt(t);
x = parseFloat(t);
原文地址:https://www.cnblogs.com/dongfangchun/p/8043601.html