亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 PHP開發(fā) Summary of common methods for dynamically setting CSS styles using JS

Summary of common methods for dynamically setting CSS styles using JS

Dec 07, 2016 am 10:51 AM

Use JS to dynamically set CSS styles. Common ones are as follows

1. Directly set the style attribute. 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 keep the - sign, use the form of square brackets element.style['text-align'] = '100px';

element.style.height = '100px';

2. Set attributes directly (can only be used for certain attributes, Related styles will be automatically identified)

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 method of changing class

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

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 file

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, insertRule

// 在原有樣式操作
document.styleSheets[0].addRule('.box', 'height: 100px');
document.styleSheets[0].insertRule('.box {height: 100px}', 0);
// 或者插入新樣式時(shí)操作
var styleEl = document.createElement('style'),
styleSheet = styleEl.sheet;
styleSheet.addRule('.box', 'height: 100px');
styleSheet.insertRule('.box {height: 100px}', 0);
document.head.appendChild(styleEl);


Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72