Home > Web Front-end > JS Tutorial > body text

How to Override the `!important` Style in JavaScript?

Patricia Arquette
Release: 2024-10-28 05:39:02
Original
842 people have browsed it

How to Override the `!important` Style in JavaScript?

Overriding the !important Style

This style declaration from an external style sheet:

<code class="css">td.EvenRow a {
  display: none !important;
}</code>
Copy after login

prevents JavaScript from using element.style.display = "inline" or element.style.display = "inline !important" to set the element back to visible.

Solution:

To override the !important style using JavaScript, there are a few options:

1. Set the 'style' Attribute:

<code class="js">element.setAttribute('style', 'display:inline !important');</code>
Copy after login

2. Modify the Style Object's cssText Property:

<code class="js">element.style.cssText = 'display:inline !important';</code>
Copy after login

3. Use the setProperty Method:

<code class="js">element.style.setProperty('display', 'inline', 'important');</code>
Copy after login

Any of these will successfully override the !important style.

The above is the detailed content of How to Override the `!important` Style in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!