今天達人講了個 css2js 的網頁,不錯用。大致上,就是原來 CSS 裡面有 ‘-’ 的,例如 ‘a-b’ 的話,要用 javascript 來 呼叫的話就是用 ‘aB’。同理,’a-b-c’ 就是 ‘aBC’。如果是 ‘abc’ 的話,那轉過去就還是 ‘abc’ ,不過也有例外:

  • float → styleFloat

另外就是有些遇到 ‘:’ 的時候,可以比照 ‘-’ 來處理:

  • text-decoration: blink → textDecorationBlink
  • text-decoration: line-through → textDecorationLineThrough
  • text-decoration: none → textDecorationNone
  • text-decoration: overline → textDecorationOverline
  • text-decoration: underline → textDecorationUnderline

在 HTML tag 裡面,雖然 javascript 可以使用 setAttribute() 來更改 tag 中裡面的設定,比方說 <a href=”lala”> 我們可以抓到這個 node 之後(比方說用 getElementsByTagName(’a')),用 node.setAttribute(’value’,'lala’)。這樣就會變成 <a value=”lala” href=”lala”> 不過如果牽涉到 style 的話,不建議這樣使用,因為可能會修改到本來存在的 style,或者在多個函式都會對這個 node 設定 style 的時候,使用 setAttribute(’style’,'its value’) 就會有 race condition 的問題發生(譬如用了 GM_xmlhttpRequest 或是類似的函式),所以比較好的方式還是使用 node.style. 來做設定。