js操作css樣式


  div.style.width="100px"  (行間樣式)

  相當於標簽內我們添加一個style屬性,設定了width值,這種寫法會給標簽大量的style屬性,以實際項目不符,我們沒有css和HTML分離;

  所以如果為了獲取css樣式

  獲取計算機計算的所有屬性

  window.getcomputedstyle()  (兩個參數,第一個是當前操作的元素  第二個是null)並且這個方法是只讀的

  可讀和可寫的區別:   可讀  只讀獲取不能修改        可寫   可以修改

  計算的:只要渲染出來了都是經過計算的

  window.getcomputedstyle (ie 6 7 8不支持這個用法)

   ie8以下的寫法為 :          ie6(元素名).currentstyle

  兼容性:

  try{}catch(error){}      (try{}嘗試的)            (catch鋪獲)      (error報錯)

  不報錯執行try里面的代碼塊   ,  報錯執行catch里面的代碼

  前提的條件:代碼必須報錯,不報錯就不能使用;

  案例:

  var css;

  try{

    css=window.getComputedStyle()

    }

  catch(e){

    css=aa.currentStyle

    }

  console.log(css)

  總結js解決兼容性的方法:

  1.   ||

  var dd=document.documentElement.clientWidth||document.body.clientWidth

  2.  if() 條件語句;

  if(window.getComputedStyle  ==(另一種寫法:window.getComputed.style in window)){

    css=window.getComputedStyle(aa,null)

  }

  else{

    css=aa.currentStyle

  }

  console.log(css)

  3.try{}catch(error){}

  前提條件:必須在報錯的條件下和if條件語句比較性能上比較差,不在萬不得已的情況下,不能使用

  案例:

    var css;

    try{

      css=window.getComputedStyle()

      }

    catch(e){

      css=aa.currentStyle

      }

    console.log(css)

  null和undefined的區別?

  null和undefined都表示沒有值,

  null(天生存在的)這個東西存在但是沒有給值;

  undefined (人為定義)這個東西壓根不存在,而是人為定義的

  案例:  console.log(aa.parentNode.parentNode.parentNode) (通過這個案例就知道元素節點的樹狀圖,最后返回的是null)

  元素節點的樹狀圖;

  document>documentElement(html)>body>tagname

  offsetLeft和offsetTop所結合運動

  滾動輪播    

  setTimeout(延時調用)

  定時器的返回值,返回是當前頁面的第幾個定時器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM