斷斷續續看了十來天,終於看完了,仍是學到些東西,這本書仍是不錯的,各方面都有涉及。javascript
補充了下以前不完善的JS 知識php
筆記通常只記必要的東西。html
var sayHello = function() { alert("hello"); }; var sayHello = () => alert('hello'); var myFunc = x => alert(x); myFunc = (x, y) => alert(x + y); myFunc = (x, y, z) => { let area = x*y + 5; let boxes = area / z; return boxes; }
var myDiv = document.getElementById("div1");
alert(history.length); history.forward(); history.backward(); history.go(-3); history.go(2); history.go("baidu.com");
var mydate = new Date(); var year = mydate.getFullYear(); var month = mydate.getMonth(); var date = mydate.getDate(); var day = mydate.getDay(); // 星期 var hours = mydate.getHours(); var minutes = mydate.getMinutes(); var seconds = mydate.getSeconds();
var d1 = new Date("October 22, 1995 10:57:22"); var d2 = new Date(95, 9, 22); var d3 = new Date(95, 9, 22, 10, 57, 0);
var mydate = new Date(); document.write(mydate.getDay()); mydate.setDate(15); document.write(mydate.getDat()); document.write(mydate.toDateString() + mydate.toTimeString());
function myRand(range) { return Math.round(Math.random() * range); }
.E 天然常數,約爲 2.718 .LN2 2 的天然對數,約爲 0.693 .LN10 10 的天然對數,約爲 2.302 .LOG2E 以 2 爲底 e 的對數,約爲 1.442 .LOG10E 以 10 爲底 e 的對數,約爲 0.434 .PI 圓周率,約爲 3.14159 .SQRT12 2 的平方根的倒數,約爲 0.707 .SQRT2 2 的平方根,約爲 1.414 node
with (Math) { var myRand = random(); var biggest = max(2, 3, 5); var height = round(11.22); }
示例 jquery
Number.isNaN(3); //false Number.isNaN(0 / 0); // true
Number.parseFloat("21.4"); // 21.4 Number.parseInt("12px",10); // 12
var name = "aa"; var course = "bb"; var myString = `hell ${name}, welcome ${course}`;
var myArray = ['111', '22']; myArray[20] = "333e"; myArray.length
接受一個函數做爲參數,並將函數依次應用與數組中的每一個元素 程序員
var myArray = [1, 2, 3, 4]; function cube(x) { console.log(x*x*x); } myArray.forEach(cube);
和 forEach 的區別在於 map 會返回和最初數組相同大小的新數組 web
var myArray = [1, 2, 3, 4]; fucntion cube(x) { return (x*x*x); } var newArray = myArray.map(cube);
var myArray = [2,3,4,5]; functionn cube(x) { console.log(x*x*x); } for (var y of myArray) { cube(y); }
<a href="http://www.baidu.com" onclick="alert('hello')">baidu</a>
// <a href="xxx" id="a1">aa</a> var myLink = document.getElementById('a1'); myLink.onclick = function() { alert("hello"); }
var myLink = document.getElementById('a1'); function sayHello() { alert("hello"); } myLink.addEventListener('click', sayHello); myLink.removeEventListener('click', sayHello);
myInputField = document.getElementById('a'); function myFunction(e) { var kc = e.keyCode; if (kc == 27) { alert("xx"); } } myInputField.addEventListener('keydown', myFunction);
var myLink = document.getElementById('a'); function refuseAccess(e) { alert("wrong"); e.preventDefault(); } myLink.addEventListener('click', refuseAccess);
function myFunction(e) { var kc = e.keyCode; e.stopPropagation(); if (kc == 27) { alert("1"); } }
function hide(elementId) { document.getElementById(elementId).style.display='none'; } window.onload = function() { setTimtout("hide('id1')", 3000); } var timer1 = setTimeout("hide('id1')", 3000); clearTimeout(timer1);
var timer1 = setInterval("updateClock()", 1000); clearInterval(timer1);
myNewObject = new Object(); myNewObject.showInfo = myFunc; myNewObject.info = 'this is a info';
myNewObject.showInfo = function() { alert(this.info); }
若是要建立一個類的多個實例,能夠建立構造函數 ajax
function Car(Color, Year, Make, Miles) { this.color = Color; this.year = Year; this.make = Make; this.odometerReading = Miles; this.setOdometer = function(newMiles) { this.odometerReading = newMiles; } } var car1 = new Car("blue", "1998", "Ford", 77777);
function Person(personName) { this.name = personName; this.info = 'a'; this.showInfo = function() { alert(this.info); } } var person1 = new Person('adam'); var person2 = new Person('hehe'); Person.prototype.sayHello = function() { alert(this.name + 'say hello'); }
function Pet() { this.animal = ""; } function Dog() { this.breed = ""; } Dog.prototype = new Pet();
function a(b, c, d) { function kk(m, n) { return m + n; } }
新引入的 class 關鍵字和 constructor
function Pet() { this.animal = ""; this.setAnimal = function(newAnimal) { this.animal = newAnimal; } } class Pet { constructor(animal) { this.animal = animal; } setAnimal(newAnimal) { this.animal = newAnimal; } }
class Pet { constructor(animal, name) { this.animal = animal; this.name = name; } get name() { return this._name; } set name(n) { n = n.charAt(0).toUpperCase() + n.slice(1).toLowerCase(); this._name = n; } }
建立的 Symbol 必定是惟一的傳的參數只是說明
var myname = Symbol('nickname'); myCat[myname] = 'heheda';
if (document.getElementById) { // do something } else { // other }
typeof
if (typeof document.getElementById == 'function') { // do something } else { // other }
childNodes 屬性
function countListItems() { var olElement = document.getElementById("toDoList"); var count = 0; for (var i = 0; i < olElement.childNodes.length; i++) { if (olElement.chiodNodes[i].nodeType == 1) count++; } } window.onload = countListItems;
var myNode = document.getElementById("id1"); alert(myode.getAttribute("title"));
removeChild()
var myDiv = document.getElementById("id1"); var myPara = document.getElementById("para2"); var removedItem = myDiv.removeChild(myPara); alert(removedItem.getAttribute("id"));
var myPara = document.getElementById("para1"); myPara.setAttribute("title", "opening paragraph");
var scr = document.createElement("script"); scr.setAttribute("src", "newScript.js"); document.head.appendChild(scr);
var x = eval(1*2); eval("a=1;b=2;document.write(a+b);"); var user = '{"user":"admin", "location": "spain"}'; var myObject = eval('(' + user + ')');
var conference = {"a": "1", "b": "2"}; alert(conference["a"]); alert(conference.a);
var myVideo = document.getElementById("vid1").play(); var myVideo = document.getElementById("vid1").pause();
var soundElement = document.createElement('audio'); soundElement.setAttibute('src', 'sound.ogg'); soundElement.play(); soundElement.currentTime = 12;
兩個對象 localStorage sessionStorage
if (typeof(Storage) != "undefined") { localStorage.setItem("key", "value"); localStorage["key"] = "value"; alert(localStorage.getItem("key")); alert(localStorage["key"]); }
var cookieDate = new Date ( 2018, 05, 15 ); var user = "heheda"; document.cookie = "username=" + escape(user) + ";expires=" + cookieDate.toUTCString();
var myRegExp = FooBar;
var myRepExp = /FooBar/; if (document.getElementById("txt").value.search(myRegExp) == -1) { alert("not found"); } else { alert("The string occurs"); }
i 不區分大小寫 g 全局匹配,而不是找到第一個匹配就中止 m 多行匹配 var aa = /stupid/gi;
var myPattern = new RegExp("Foobar"); var result = /boy/.exec("heheda"); console.log(result[0]); myPattern.test("this is a test"); var result = myPattern.exec(myString);
match search replace split
var myString = "heheda dada"; var outString = myString.match(/\d+/g); var myString = "1 ;2 3;4 5"; var outString = myString.split(/\s*;\s*/); myString.replace(/stupid/ig, "cupid");
用一個函數做爲 replace() 的參數
function CtoF(match) { return ((match * 9) / 5) + 32; } var myString = "for 1 and 5"; myString = myString.replace(/\d+/g, CtoF);
function sayHi(visitor) { let msg = visitor + ' says: hello world!'; return function logMessage() { console.log(msg); } }
function sayHi(visitor) { let msg = visitor + ' says: '; return function logMessage(extra) { msg = msg + extra; console.log(msg); } } var helloPhil = sayHi("Phil"); var helloSue = sayHi("Sue");
function sayHi(visitor) { let msg = visitor + ' says: hello world'; return function logMessage() { console.log(msg); } } var helloPhil = sayHi("Phil"); helloPhil()
function fun1(x) { alert(x); } export func1;
function convertCtoF(c) { return (c*1.8) + 32; } function convertFtoC(c) { return (f-32) / 1.8; } export {convertCtoF, convertFtoC} export var a = "something"; export function func1() {console.log("hello");};
export var distance = arr[1]; export {arr as routeProperties };
function mmToInches(d) { return d/25.4; } export default mmToInches;
import {convertCtoF, convertFtoC} from './tempConvert.js';
相似於 window.onload
$(document).ready(function() { // code });
html() 相似於 innerHTML
var htmlContent = $("#elem").html(); $("#elem").html("<p>here </p>");
text() 只是獲取文本內容
var textContent = $("#elem").text() $("#elem").append("<p>hehe </p>");
attr()
var title = $("#elem").attr("title"); $("elem").attr("title", "new title");
show()
$("div").show(); $("#elem").show("fast", function() { // 操做 });
hide()
$("#elem").hide("slow", function() { // 隱藏以後的操做 });
toggle()
$("#elem").toggle(1000, function() { // xx });
jQuery 大多數方法都返回一個 jQuery 對象
$("#elem").fadeOut().fadeIn(); $("#elem").text("Hello ").fadeOut().fadeIn();
經常使用方法
$("a").click(function() { // }); function hello() { alert("hello"); } $("a").click(hello); $("a").on("click", hello);
load()
$(function() { $("#elem").load("newContent.html"); }); $(function() { $("#elem").load("newContent.html #info"); });
get() post()
$.get("serverScript.php", {param1: "value1", param2: "value2"}, function(data) { alert("server response: " + data); });
Author: cat
Created: 2019-11-13 Wed 22:40
原文出處:https://www.cnblogs.com/junmoxiao/p/11853871.html