JavaScript FAQ(十八)—— CSS__Java

來源:互聯網
上載者:User

 十五、層疊樣式表(CSS)

 

1.  應用樣式表(Applying Stylesheets)

Q:我如何在頁面上應用另外一個樣式表。

A:瀏覽器通過一個或者多個LINK命令(主要是在頁面的HEAD地區)載入樣式表(CSS檔案),例如:

<LINK rel="stylesheet" type="text/css" href="styleA.css"><LINK rel="stylesheet" type="text/css" href="styleB.css"><LINK rel="stylesheet" type="text/css" href="styleC.css">

JavaScript通過使用集合document.styleSheets訪問樣式表。你可以通過將document.styleSheets[i].disabled的值設為false來應用一個樣式表。將disabled設為ture就可以禁用那些你當前不需要的樣式表。下面的函數應用第i個樣式表,而禁用其他所有的樣式表:

function applyStyle(k) { if (document.styleSheets) {  var nStyles = document.styleSheets.length;  for (var i=0;i<nStyles;i++) {   if (i==k) document.styleSheets[i].disabled = false;   else      document.styleSheets[i].disabled = true;  } }}

現在試一下(譯者註:可以在原文上測試):

應用樣式A(藍色文本)——調用applyStyle(0)

應用樣式B(灰色文本)——調用applyStyle(1)

應用樣式C(黑色文本)——調用applyStyle(2)

 

 

2. 滑鼠指標樣式(Mouse Cursor Styles)

Q:如何通過JavaScript改變滑鼠指標樣式。

A:當今多數瀏覽器支援一下指標樣式(將滑鼠移動到樣式名稱上以觀察指標樣式改變為那個樣式):

auto        move           no-drop      col-resizeall-scroll  pointer        not-allowed  row-resizecrosshair   progress       e-resize     ne-resizedefault     text           n-resize     nw-resizehelp        vertical-text  s-resize     se-resize   inherit     wait           w-resize     sw-resize         

 

在Windows Internet Explorer 6.0或者更新版本,上面的指標樣式如下:


 要通過指令碼改變某一個元素的滑鼠指標樣式,你可以把元素的屬性element.style.cursor設為上面的任何一個值。(另外一種不用JavaScript的方法,可以在元素的HTML標籤中使用屬性style="cursor:value;"):

function setCursorByID(id,cursorStyle) {if (document.getElementById) {  if (document.getElementById(id).style) {   document.getElementById(id).style.cursor=cursorStyle;  }}}


在下面的示範中你可以為高亮元素(Element 1 和 Element 2)改變指標樣式。在這個例子中,當你點擊超連結help, wait, crosshair, text, default, move, 或者 pointer時,指標就會被在整個高亮元素上改為相應的樣式,上面的例子中函數setCursorByID將會被使用到。當你點擊超連結auto時,特定元素的指標就會被瀏覽器改為原來的樣式。

 Element 1  Change cursor to any of these styles: help wait move crosshair text default pointer auto Element 2  Change cursor to any of these styles: help wait move crosshair text default pointer auto

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.