標籤:js
表徵圖網站 http://fontawesome.io/icon
<i class="fa fa-sort" aria-hidden="true"></i>
3.21
函數
匿名函數
自執行函數 建立函數並自動執行
(function(arg){ 形參
console.log(arg);
})(1) 實參
序列化 字串變數
JSON.stringify() 序列化 從一個對象解析出字串
JSON.parse() 反序化 字串中解析出json對象
轉義
- encodeURI( ) URI中的逸出字元
- decodeURI( ) URl中未轉義的字元
將資料經過轉義後,儲存在cookie
val = eval(運算式)
時間
var d = new Date()
d.getMinutes()
d.setMinutes()
範圍
範圍為可訪問變數,對象,函數的集合。
JavaScript 變數生命週期在它聲明時初始化。
局部變數在函數執行完畢後銷毀。
全域變數在頁面關閉後銷毀。
-
以函數作為範圍
-
函數的範圍在函數未被調用之前,就已經建立
-
函數的範圍 存在 範圍鏈,並且也是在被調用之前建立。。先找自己的,自己沒有找上一層,沒有再找外層。
-
函數內的局部變數會提前聲明,不進行賦值
function func() {
var xo = ‘hequan‘;
function inner() {
console.log(xo);
}
return inner;
}
var ret = func()
ret()
hequan
function func() {
console.log(xo);
var xo = "hequan";
}
func()
undefined
物件導向
function foo(n) {
this.name=n;
this.sayName = function () {
console.log(this.name)
}
}
var obj1 = new foo("hq")
obj1.name
obj1.sayName()
原形:
function Foo(n) {
this.name = n;
}
Foo.prototype = {
‘sayName‘:function () {
console.log(this.name)
}
}
obj1 = new Foo(‘we‘);
obj1.sayName()
尋找
直接尋找
var obj = document.getElementById(‘I1‘)
間接尋找
innerText 文本
innerHtml 全內容
obj.value 擷取值
樣式操作
.className
.classList.add .remove
.style.color
屬性操作
obj.setAttribute("屬性","值")
建立標籤 並添加到HTML
字串
insertAdjacentHTML("beforeEnd",obj)
對象的方式
document.createElement(‘div‘)
提交表單
document.geElementById(‘form‘).submit()
其他
console.log 輸出框
alert 彈出框
confirm 確認框 返回true false
// URL和重新整理
location.href 擷取URL
location.href = "url" 重新導向
location.reload() 重新載入
// 定時器
setInterval 多次定時器
clearInterval 清除多次定時器
setTimeout 單次定時器
clearTimeout 清除單次定時器
事件
onclick,onblur,onfocus
document.getElementById 根據ID擷取一個標籤
document.getElementsByName 根據name屬性擷取標籤集合
document.getElementsByClassName 根據class屬性擷取標籤集合
document.getElementsByTagName 根據標籤名擷取標籤集合
綁定事件兩種方式:
直接綁定 onclick="xx()"
先擷取DOM對象,然後進行綁定
<div id = ‘test‘><button type="button">Click Me!</button></div>
<script>
var mydiv = document.getElementById(‘test‘)
console.log(mydiv)
mydiv.onclick = function () {
console.log(‘asdw‘);
}
</script>
this 當前觸發事件的標籤
第一種綁定方式
onclick="Chang(this)"
function Chang(self){
//self 當前點擊的標籤
}
第二種綁定方式
<input id=‘i1‘ type=‘button‘>
document.getElementById(‘i1‘).onclick = function(){
//this 代之當前點擊的標籤
}
本文出自 “何全” 部落格,請務必保留此出處http://hequan.blog.51cto.com/5701886/1909384
day16 js+dom