取得CSS 規則的內容
了解如何存取CSS 規則的值對於動態樣式或操縱CSS 的呈現非常有價值你的元素。
要以內嵌樣式格式傳回包含 CSS 規則所有內容的字串,可以使用下列方法:
function getStyle(className) {<pre class="brush:php;toolbar:false">var cssText = ""; var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules; for (var x = 0; x < classes.length; x++) { if (classes[x].selectorText == className) { cssText += classes[x].cssText || classes[x].style.cssText; } } return cssText;
}
alert(getStyle('.test'));
在此程式碼中:
以上是如何在 JavaScript 中檢索 CSS 規則的內容?的詳細內容。更多資訊請關注PHP中文網其他相關文章!