Home >Web Front-end >Front-end Q&A >How to modify css style with javascript

How to modify css style with javascript

青灯夜游
青灯夜游Original
2021-09-08 18:17:422631browse

Modification method: 1. Use "Element.style.Attribute name="value""; 2. Use "Element.setAttribute('Attribute name','Value')"; 3. Use "Element. setAttribute('style','Attribute name: value')"; 4. Use "element.className='value'".

How to modify css style with javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. Directly set the attribute of style In some cases, use this setting! important value is invalid

If the attribute has a '-' sign, write it in camel case (such as textAlign) If you want to retain the - sign, use the form of square brackets element.style['text-align'] = '100px';

element.style.height = '100px';

2. Set the attribute directly (can only be used For some attributes, related styles will be automatically recognized)

element.setAttribute('height', 100);
element.setAttribute('height', '100px');

3. Set the attributes of style

element.setAttribute('style', 'height: 100px !important');

4. Use setProperty If you want to set !important, it is recommended to use this method to set the third parameter

element.style.setProperty('height', '300px', 'important');

5. Change class, such as JQ’s change class related method

Because JS cannot obtain the pseudo-element of css, so you can dynamically change the style of the pseudo-element by changing the class of the pseudo-element's parent

element.className = 'blue';
element.className += 'blue fb';

6. Set cssText

element.style.cssText = 'height: 100px !important';
element.style.cssText += 'height: 100px !important';

7. Create and introduce new css style files

  function addNewStyle(newStyle) {
            var styleElement = document.getElementById('styles_js');

            if (!styleElement) {
                styleElement = document.createElement('style');
                styleElement.type = 'text/css';
                styleElement.id = 'styles_js';
                document.getElementsByTagName('head')[0].appendChild(styleElement);
            }
            
            styleElement.appendChild(document.createTextNode(newStyle));
        }

        addNewStyle('.box {height: 100px !important;}');

8. Use addRule and insertRule

// 在原有样式操作
        document.styleSheets[0].addRule('.box', 'height: 100px');
        document.styleSheets[0].insertRule('.box {height: 100px}', 0);

        // 或者插入新样式时操作
        var styleEl = document.createElement('style'),
            styleSheet = styleEl.sheet;

        styleSheet.addRule('.box', 'height: 100px');
        styleSheet.insertRule('.box {height: 100px}', 0);

        document.head.appendChild(styleEl);

[Recommended learning: javascript Advanced Tutorial

The above is the detailed content of How to modify css style with javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn