可能重複:
How to change/remove CSS classes definitions at runtime?如何獲取樣式元素中的CSS代碼?
<!DOCTYPE html>
<meta charset="utf-8">
<title>An HTML5 Document</title>
<style>
body {
background: red;
}
</style>
如何通過JS獲得在style
元素的CSS代碼?謝謝。
可能重複:
How to change/remove CSS classes definitions at runtime?如何獲取樣式元素中的CSS代碼?
<!DOCTYPE html>
<meta charset="utf-8">
<title>An HTML5 Document</title>
<style>
body {
background: red;
}
</style>
如何通過JS獲得在style
元素的CSS代碼?謝謝。
var styleElement = document.createElement("style");
styleElement.innerHTML = "body { \n\
background: red; \n\
}";
document.body.appendChild(styleElement);
你有什麼需要得到什麼?它是DOM元素的樣式嗎? – DavidS 2012-08-10 18:17:05
看看這裏,看看這個解決方案是否適合你: http://stackoverflow.com/questions/707565/how-do-you-add-css-with-javascript – robbieAreBest 2012-08-10 18:17:18