」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何使用 jQuery 即時動態更改 CSS 類別規則?

如何使用 jQuery 即時動態更改 CSS 類別規則?

發佈於2024-11-19
瀏覽:346

How can I dynamically change CSS class rules in real time with jQuery?

使用 jQuery 動態更改 CSS 類別規則

您的查詢涉及兩個方面:

1。即時修改類別規則

僅靠 jQuery 無法動態變更 CSS 類別規則。但是,您可以利用文件物件的 styleSheets 屬性直接存取 CSS 規則。

代號:

document.getElementById("button").onclick = function() {
    var ss = document.styleSheets;

    for (var i = 0; i 

2.將類別更改保存到文件

要將類別更改保存到文件,您需要提取CSS 規則並透過Ajax 請求將它們發送到伺服器。伺服器端實作涉及使用修改後的規則建立或更新檔案。

附加說明:

  • 為了相容於IE6,請使用document.styleSheets 而非document.styleSheets.
  • 要取得CSS規則,請存取樣式表物件的rules屬性。
  • 使用cssText屬性來設定或修改 CSS 規則。

參考:

  • document.styleSheets (Mozilla):https://developer.mozilla.org /en-US/docs/Web /API/Document/styleSheets
  • styleSheet 物件(Mozilla): https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet
  • CssRule 物件(Mozilla):https://developer.mozilla.org/en-US/docs/Web /API/CSSRule
  • document.styleSheets (MSDN): https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/dom/document.stylesheets
  • CssRule物件(MSDN):https://docs .microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/dom/cssrule
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3