=====================
toString
Numble()
paseint()
pasefloat()
str.charAt()
str.indexof()
str.lastIndexof()
======================
var name='tom' //函數外部聲明的變量爲全局作用域
funciton hello(){
name='jack' ; //沒有加var就是全局變量,並更改了之前外部的name的值
age=20; //沒有加var就是全局變量,在函數內部聲明瞭一個新的全局變量 不推薦會污染 全局變量
var name='tom' //var聲明的變量爲局部變量
}
====================
//通過構造函數創建
var obj1= new Object()
obj1.name='tom'
obj1.age=20
obj1.sayinfo=function(){console.log('姓名是:'+this.name)}
//通過{}創建
var obj2= {}
obj2.name='tom'
obj2.age=20
obj2.sayinfo=function(){console.log('姓名是:'+this.name)}
//通過字面量創建
var obj3={ name:'tom', age:'20',sayinfo:function(){console.log('姓名是:'+this.name}}
1:
document.getElementsByTagName()
document.getElementsByClassName()
document.getElementById()
document.querySelector()
document.querySelectorAll()
-------------------------------------
節點類型:元素節點 屬性節點 文本節點
-------------------------------------
nodeType:節點類型 1:元素節點 2:屬性節點 3:文本節點
nodeName:節點名稱
nodeValue:節點的值
---------------------------------
parentNode:父節點
childNodes:子節點
previousSibing:前一個節點
nextSibing:下一個節點
firstChild:第一個子節點
lastChild:最後一個子節點
hasChildNodes():是否有子節點
--------------------------
//點操作符
h2.id
h2.style.color="red"
//元素的操作方法
h2.getAttribute('id')
h2.setAttribute('id')
h2.removeAttribute('id')
//元素的屬性
h2.attributes
h2.attributes[0]
h2.attributes['id']
h2.attributes.removeNamedItem('id')
=============================
//元素節點的增刪改查
//創建元素節點
var li=document.createElement("li")
//創建文本節點
var txt=document.createTextNode("jquery")
//給創建元素添加文本節點
li.appendChild(txt)
//添加到html頁面的ul元素節點後面
var ul =document.getElementsByClassName('list')
ul.append(li)
//添加到指定位置:insertBefore(節點,位置)
ul.insertBefore(li,ul.childNodes[5])
//刪除字節:父節點.removeChild(節點)
ul.removeChild(li)
============================
//javascript事件是文檔或者瀏覽器窗口中發生的,特定的交互瞬間。
窗口事件:onload
鼠標時候:onclick
鍵盤事件:onkeydown
焦點事件:onfocus
//事件綁定的三種方式
//1、綁定標籤屬性 οnclick="js代碼"
<h2 οnclick="console.log('我是h2,我被點擊了')" >你好</h2>
//2、綁定對象屬性 object.οnclick=fuction(event){代碼}
<script>
window.οnlοad=function(){
var h2=document.getElementsByTagName('h2')[0];
h2.οnclick=function(){
console.log('我是h2,我被點擊了');
}
}
</script>
//3、綁定對象方法
addEventListener(事件,函數,false) //添加事件
removeEventListener(事件,函數,false) //移除事件
<script>
window.οnlοad=function(){
var h2=document.getElementsByTagName('h2')[0];
var showMess=function(){
console.log('我是h2,我被點擊了');
}
h2.addEventListener('click',showMess,false) //添加事件
h2.removeEventListener('click',showMess,false) //移除事件
}
</script>
//事件冒泡:縣-》市=》省=》中央 由下向上
<script>
window.οnlοad=function(){
var li=document.getElementsByTagName('li');
var ul=document.getElementsByTagName('ul')[0];
document.body.οnclick=function (event){
alert('body 被點擊了');
}
ul.οnclick=function (event){
alert('ul 被點擊了')
}
for( var i=0; i<li.length; i++){
li[i].οnclick= function(event){
alert('li 被點擊了');
//阻止冒泡 event.cancelBubble=true ; //終止冒泡方法1
//阻止冒泡 event.stopPropagation(); //終止冒泡方法2
}
}
}
</script>
//事件捕捉:中央=》省=》市=》縣 由上向下