1. 程式人生 > >js的操作及css樣式

js的操作及css樣式

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)

1. try{} catch(err){}

必須在報錯的條件下,和if  else比較效能上比較差,不在萬不得以的情況下不使用

只讀 可寫

只讀: 只能獲取不能修改

可寫:可以修改的

Null和undefined的區別

Null和undefined都表示沒有值

Null 是這個東西是天生存在的但是沒給值。

比如

var aa=document.getElementById("aa")

console.log(aa.parentNode.parentNode.parentNode.parentNode) null

Undefined 這個東西壓根就不存在的是人為定義的並且沒賦值。

  1. var a;undefined
  2. Div.aa undefined

元素節點的樹狀圖

Document>documentElement>body>tagName

offsetLeft/offsetTop 結合運動

滾動輪播