在JavaScript中添加CSS樣式有多種方法,以下是一些常見(jiàn)的做法:
1、使用style屬性:
- 你可以直接在HTML元素上使用style
屬性來(lái)添加CSS樣式。
```html
<div id="myDiv" style="color: red; background-color: blue;">Hello World!</div>
```
- 也可以在JavaScript中動(dòng)態(tài)設(shè)置樣式:
```javascript
document.getElementById('myDiv').style.color = 'red';
document.getElementById('myDiv').style.backgroundColor = 'blue';
```
2、使用CSSStyleSheet:
- 可以通過(guò)CSSStyleSheet
接口創(chuàng)建或修改樣式表。
```javascript
var styleSheet = document.styleSheets[0]; // 獲取***個(gè)樣式表
styleSheet.insertRule('div { color: red; }', 0); // 在樣式表中添加規(guī)則
```
3、使用createElement和appendChild:
- 可以創(chuàng)建一個(gè)新的style
元素,并將其添加到文檔中:
```javascript
var styleElement = document.createElement('style');
styleElement.innerHTML = 'div { color: red; }';
document.head.appendChild(styleElement);
```
4、使用MutationObserver:
- 如果需要在樣式變化時(shí)做出響應(yīng),可以使用MutationObserver
來(lái)監(jiān)視樣式表的更改:
```javascript
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
console.log('A child node has been added or removed.');
} else if (mutation.type === 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute has changed.');
} else if (mutation.type === 'style') {
console.log('A style has changed.');
} else if (mutation.type === 'characterData') {
console.log('Character data has changed.');
} else if (mutation.type === 'document') {
console.log('The entire document has been replaced.');
} else if (mutation.type === 'document-charset') {
console.log('The document\'s character set has changed.');
} else if (mutation.type === 'document-title') {
console.log('The document\'s title has changed.');
} else if (mutation.type === 'document-language') {
console.log('The document\'s language has changed.');
} else if (mutation.type === 'document-version') {
console.log('The document\'s version has changed.');
} else if (mutation.type === 'document-content-type') {
console.log('The document\'s content type has changed.');
} else if (mutation.type === 'document-root-element') {
console.log('The document\'s root element has changed.');
} else if (mutation.type === 'document-complete') {
console.log('The document has completed loading.');
} else if (mutation.type === 'document-visibility-change') {
console.log('The document\'s visibility has changed.');
} else if (mutation.type === 'document-language-change') {
console.log('The document\'s language has changed.');
} else if (mutation.type === 'document-title-change') {
console.log('The document\'s title has changed.');
} else if (mutation.type === 'document-charset-change') {
console.log('The document\'s character set has changed.');
} else if (mutation.type === 'document-version-change') {
console.log('The document\'s version has changed.');
} else if (mutation.type === 'document-content-type-change') {
console.log('The document\'s content type has changed.');
} else if (mutation.type === 'document-root-element-change') {
console.log('The document\'s root element has changed.');
} else if (mutation.type === 'document-complete-change') {
console.log('The document has completed loading.');
} else if (mutation.type === 'document-visibility-change-change') {
console.log('The document\'s visibility has changed.');
} else if (mutation.type === 'document-language-change-change') {
console.log('The document\'s language has changed.');
} else if