• yam天空
  • 天空部落
  • 新聞
  • 登入 註冊 網誌隨便逛
  • 加入天空部落
  • 華文部落格大賞結果出爐!

網誌 相簿 影音 PK吧! Honda嬉遊趣
即時新聞 影音新聞 新聞專輯 政治新聞 財經新聞 娛樂新聞 運動新聞 兩岸新聞 科技新聞
管理介面 發表網誌 發表日記 上傳相片 上傳影音 管理留言
推薦這個部落格: 192

A HA! 心情雜物間

專供個人碎碎念的心得小屋,或是好物分享~~ ^_^

日記 |網誌 |相簿 |好友 |留言板
[展覽] 派樂地(parody) | 主頁 | [CSS] opacity 透明度
September 1, 2009
Get CSS properties by JavaScript以文找文
csylvia 在天空部落發表於11:44:11 | JavaScript
鼓勵此網誌:0 

今天碰到個特別的需求:利用 JavaScript 取得 CSS 的屬性資訊。


我知道如何用 JavaScript 去設定 CSS:

 

1: document.getElementById("xxx").className = "yyy";
2: document.getElementById("xxx").style.backgroundColor = "#FFFFFF";

 

在這篇中還有寫到如何直接設定 CSS file:

 

1: <link rel="stylesheet" type="text/css" id="css" href="firefox.css" />
2: <span onClick="javascript:document.getElementById('css').href='ie.css'">點我改變樣式</span>

 

網路上也有 CSS 和 JavaScript 的屬性對應表:CSS Properties to JavaScript Reference,
這邊也複製一份存放,可點擊這邊展開觀看。

CSS PropertyJavaScript Reference
backgroundbackground
background-attachmentbackgroundAttachment
background-colorbackgroundColor
background-imagebackgroundImage
background-positionbackgroundPosition
background-repeatbackgroundRepeat
borderborder
border-bottomborderBottom
border-bottom-colorborderBottomColor
border-bottom-styleborderBottomStyle
border-bottom-widthborderBottomWidth
border-colorborderColor
border-leftborderLeft
border-left-colorborderLeftColor
border-left-styleborderLeftStyle
border-left-widthborderLeftWidth
border-rightborderRight
border-right-colorborderRightColor
border-right-styleborderRightStyle
border-right-widthborderRightWidth
border-styleborderStyle
border-topborderTop
border-top-colorborderTopColor
border-top-styleborderTopStyle
border-top-widthborderTopWidth
border-widthborderWidth
clearclear
clipclip
colorcolor
cursorcursor
displaydisplay
filterfilter
fontfont
font-familyfontFamily
font-sizefontSize
font-variantfontVariant
font-weightfontWeight
heightheight
leftleft
letter-spacingletterSpacing
line-heightlineHeight
list-stylelistStyle
list-style-imagelistStyleImage
list-style-positionlistStylePosition
list-style-typelistStyleType
marginmargin
margin-bottommarginBottom
margin-leftmarginLeft
margin-rightmarginRight
margin-topmarginTop
overflowoverflow
paddingpadding
padding-bottompaddingBottom
padding-leftpaddingLeft
padding-rightpaddingRight
padding-toppaddingTop
page-break-afterpageBreakAfter
page-break-beforepageBreakBefore
positionposition
floatstyleFloat
text-aligntextAlign
text-decorationtextDecoration
text-decoration: blinktextDecorationBlink
text-decoration: line-throughtextDecorationLineThrough
text-decoration: nonetextDecorationNone
text-decoration: overlinetextDecorationOverline
text-decoration: underlinetextDecorationUnderline
text-indenttextIndent
text-transformtextTransform
toptop
vertical-alignverticalAlign
visibilityvisibility
widthwidth
z-indexzIndex

 

 

JavaScript 去設定 CSS 其實很簡單,
但要如何利用 JavaScript 獲取 CSS?這就是個問題了。

所幸上網查一下後就得到答案~~ ^^

本來在「how to get CSS information with javascrpt」&「Javascript: Get a value set by CSS - Xtreme Visual Basic Talk」兩篇得到的資訊是

 

 Ive had a search around the net and it seems i can use "currentStyle" instead of just "style". Aparently it isnt suported in a lot of browsers but its supported in IE6. Thats fine for me because im only writing this to suit IE6.

 

以為只有 IE6 可以這樣做,沒望了,
但後來又找到「JavaScript - Get Styles」、「JavaScript tutorial - DOM CSS」這兩篇,
試試看後總算解決這個問題!

解答就是

 

 1: var obj = document.getElementById("xxx");
 2: var w, h;
 3: 
 4: // for IE
 5: if( document.defaultView ) {
 6: 	w = document.defaultView.getComputedStyle(obj,null).getPropertyValue("width");
 7: 	h = document.defaultView.getComputedStyle(obj,null).getPropertyValue("height");
 8: }
 9: // for Mozilla
10: else if( obj.currentStyle ) {
11: 	w = obj.currentStyle.width;
12: 	h = obj.currentStyle.height;
13: }
 

 

 有網路真好!XD

 

 

 

【Set CSS】

  •  JavaScript修改CSS樣式 - wowbox blog (網頁設計知識庫)

 

【Get CSS】

  • JavaScript - Get Styles
  • JavaScript tutorial - DOM CSS
  • JavaScript: getComputedStyle() for CSS ‧ mozillaZine Forums
  • how to get CSS information with javascrpt
  • Javascript: Get a value set by CSS - Xtreme Visual Basic Talk

 

【Other】

  • CSS Properties to JavaScript Reference
  • 用 javascript 获取页面上有选择符的 CSS 规则 - javascript, css, style, 选择符, 样式, stylesheet

 

留言 (1) | 引用 (0) | 人氣 () | 轉寄
此分類上一篇:Make scrolling content in side column of blog | 主頁 | 此分類下一篇:document.createElement()
引用 (你可以針對此文寫一篇屬於自己的blog/想法,並給作者一個通告)
引用
留言 (1筆)
1.
are you a computer engineer?
板主回覆:
Yes, I am.
sampra 於 2009-09-29 01:27:14 留言 |

發表你的留言 (字數限制 最多 2000 個中文字)
私密留言: 是 否
Name:





是 否
內容:
系統公告
系統工具


Sylvia 小檔案
個人圖檔
ID:csylvia
暱稱:Sylvia
地區:臺北市
  • 訂閱 |
    • 我要訂閱此部落格的
    • 日記
    • 網誌
    • 相簿
  • 好友 |
    • 好友功能
    • 觀看好友列表
    • 觀看人緣列表
  • 人氣 |
  • 簡介 

文章分類
  • 生活資訊 (6)
    • 螢養保健 (11)
    • 小辭典 (13)
    • 飲食美味 (2)
    • 美容美髮 (2)
    • 體操按摩 (5)
    • 有用資訊 (10)
  • 資源連結 (4)
    • 資源整理 (12)
    • 噗浪 (8)
    • 介紹&推薦 (45)
    • BLOG小玩意 (44)
  • 真人電影 (4)
    • 電影介紹&推薦 (20)
    • 西洋電影 (35)
    • 東洋電影 (2)
    • 國片電影 (4)
  • 語言筆記 (3)
    • 英文 (4)
    • 其他語言 (1)
    • 日文 (6)
  • 創作欣賞 (7)
    • 串珠飾品 (12)
    • 毛線球 (2)
    • 文章 (1)
    • 設計 (2)
    • 不織布 DIY (5)
    • 其他創作 (1)
    • CG (8)
  • 電視影集 (3)
    • 綜藝節目 (7)
    • 紀錄片/連續劇 (11)
    • 經典影集 (12)
  • 科技資訊 (19)
    • C/C++ (58)
    • AJAX (37)
    • Code::Blocks 相關 (29)
    • WM5.0 (26)
    • Symbian (26)
    • ActiveX (22)
    • AIR (2)
    • ASP (9)
    • JavaScript (29)
    • FLASH (9)
    • 網頁相關 (8)
    • PHP (3)
    • Database (3)
    • 其他 (29)
    • Install (2)
    • SOAP (1)
    • VLC (14)
    • Linux 相關 (2)
    • Batch File (8)
  • 動漫遊戲 (3)
    • 西洋動漫 (9)
    • 其他動漫 (1)
    • 日本動漫 (22)
  • Amway (3)
    • 其他筆記 (0)
    • 智囊團 (0)
    • 紀錄 (4)
  • 個人記事 (6)
  • 書籍藝文 (29)
  • 話題探討 (28)
csylvia的最新的回應
  • Wesley:
    好久以前的東西,都忘光...
  • Wesley:
    妳真是好人
  • jamie:
    我也有類似的經驗,...
  • 小豬仔:
    Hi Sylvia,...
  • Anne:
    NANA...
  • Anne:
    嗯. ...
  • Anne:
    有去租來看過. ...
  • Anne:
    不過"窈窕野...
  • 小鼻:
    哈~!妳超力害的 ...
  • 小豬仔:
    Hello...
網站集結
迷你窩 miniworld
aNobii 書櫃
珍惜台灣
天下雜誌綠色行動4力串聯貼紙



台灣是我的國家

台灣貨‧暢其流
該推就是要推

禁虐

搶救生命 棄兒不捨!
天下雜誌台灣不願面對的真相貼紙



Chine Free

力挺泰山串聯貼紙
天氣
台北
-
RANKING
Google PageRank Checker



Free PageRank Meter for blog.webs-tv.net/csylvia
人氣指數
當日人次:
累積人次:
網誌搜尋
搜尋:
參觀者
RSS 訂閱


RSS 訂閱
RSS2
ATOM
贊助商
其它資訊
本部落所刊登之內容,皆由作者個人所提供,不代表 yam 天空 本身立場。
POWERED BY
POWERED BY 天空部落
會員登入│免費註冊