1.Js操作css樣式 div.style.width=”100px”.在div標簽內我們添加了一個style屬性,並設定 了width值。這種寫法會給標簽帶來大量的style屬性,跟實際項目是不符。 我們沒有讓css和html分離。 所以如果是為了獲取css樣式 window.getCompute ...
1.Js操作css樣式
div.style.width=”100px”.在div標簽內我們添加了一個style屬性,並設定
了width值。這種寫法會給標簽帶來大量的style屬性,跟實際項目是不符。
我們沒有讓css和html分離。
所以如果是為了獲取css樣式
window.getComputedStyle() 獲取經過電腦計算的所有屬性
就是只要渲染出來的都是經過計算的。
getComputedStyle()第一個參數是當前元素,第二個一般我們寫null
並且這個方法是只讀,
Ie6-8 不支持這個用法,ie的是用currentStyle
2.try{
}catch(error){} 不報錯執行try裡面的代碼塊,報錯執行catch裡面的代碼塊。
前提條件是報錯,如果不是報錯不能使用
var csss;
try{
csss=window.getComputedStyle(aa,null)
}catch(e){
csss=aa.currentStyle
}
console.log(csss)
總結
Js解決相容的方法
1.||
var dd=document.documentElement.clientWidth||document.body.clientWidth
2.if()else{}
if(window.getComputedStyle){
csss=window.getComputedStyle(aa,null)
}else{
csss=aa.currentStyle
}
console.log(csss)
3.try{} catch(err){}
必須在報錯的條件下,和if else比較性能上比較差,不在萬不得以的情況下不使用
只讀 可寫的區別:
只讀: 只能獲取不能修改
可寫:可以修改的
null和undefined的區別
null和undefined都表示沒有值
1.null 是這個東西是天生存在的但是沒給值。
如果我們需要清除瀏覽器變數的記憶體需要賦值null
比如 var aa=document.getElementById("aa")
console.log(aa.parentNode.parentNode.parentNode.parentNode) null
2.undefined 這個東西壓根就不存在的是人為定義的並且沒賦值。
1)var a;undefined
2)div.aa undefined