如何使用JavaScript刪除CSS屬性?

In this article, we'll see how we can remove a CSS property from a certain element using JavaScript? We can remove only those properties that we assign ourselves and the pre-default ones cannot be removed by this method.

在本文中,我們將看到如何使用JavaScript從某個元素中刪除CSS屬性? 我們只能刪除分配給我們的屬性,而默認屬性不能被此方法刪除。

HTML:

HTML:

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Removing CSS property</title>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</head>
<style>
body {
background: silver;
}
h2 {
background: wheat;
padding: 10px;
}
.btn {
background: tomato;
}
.text {
font-weight: 500;
}
</style>
<body>
<div class="container">
<h2>Let's remove some css!</h2>
<div class="container">
<button class="btn">Just a random button!</button>
<p class="text">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Tempora quis asperiores dicta quos laborum laboriosam perferendis ab veniam odit saepe, obcaecati officiis iure iste voluptates at quod commodi cupiditate voluptas!</p>
</div>
</div>
</body>
<script>
</script>
</html>

Output

輸出量

remove CSS property using JavaScript (1)

We can call on a certain DOM selector's style property and use the remove property method to remove that particular CSS property. First, let's get a reference to all the DOM elements.

我們可以調用某個DOM選擇器的style屬性,并使用remove屬性方法刪除該特定CSS屬性。 首先,讓我們參考所有DOM元素。

<script>
const heading = document.querySelector('h2');
const button = document.querySelector('.btn');
const para = document.querySelector('.text');
</script>

Now let's try to remove the background property from our heading,

現在,讓我們嘗試從標題中刪除background屬性,

heading.style.removeProperty("background");

Oops! That didn't quite work, did it? There's a simple explanation. We are loading our scripts when our whole page loads and the styles are written inside our stylesheet. Even if we remove a certain property using JavaScript, it wouldn't make any difference since that DOM element is also hooked to a stylesheet and we aren't removing any kind of CSS that we have written. Let's refactor our code a little bit now, let's clear out styles and apply those styles using JavaScript,

糟糕! 那不是很有效,對嗎? 有一個簡單的解釋。 當整個頁面加載并且樣式寫在樣式表中時,我們正在加載腳本。 即使我們使用JavaScript刪除某個屬性,也不會有任何區別,因為該DOM元素也被掛鉤到樣式表,并且我們也不會刪除我們編寫的任何CSS。 現在,讓我們重構一下代碼,清除樣式并使用JavaScript應用這些樣式,

<style>
/* body{
background: silver;
}
h2{
background: wheat;
padding: 10px;
}
.btn{
background: tomato;
}
.text{
font-weight: 500;
} */
</style>

Output

輸出量

remove CSS property using JavaScript (2)

As you can see now all our styles are removed. Let's add them back in our JavaScript,

如您所見,我們的所有樣式均已刪除。 讓我們將它們重新添加到JavaScript中,

<script>
const heading = document.querySelector('h2');
const button = document.querySelector('.btn');
const para = document.querySelector('.text');
heading.style.background = "wheat";
heading.style.padding = "10px";
document.querySelector('body').style.background = "silver";
button.style.background = "tomato";
para.style.fontWeight = "500";
</script>

Output

輸出量

remove CSS property using JavaScript (3)

And we have our styles back! Great. Now let's try removing them using our JavaScript,

我們的風格又回來了! 大。 現在,讓我們嘗試使用我們JavaScript刪除它們,

heading.style.removeProperty("background");

Output

輸出量

remove CSS property using JavaScript (4)

Our heading no longer has a wheat background! Great. Let's remove all the CSS properties inside a function and see if we get back the same unstyled page.

我們的標題不再具有小麥背景! 大。 讓我們刪除函數內的所有CSS屬性,看看是否返回相同的未樣式化頁面。

function removeProperties() {
document.querySelector('body').style.removeProperty("background");
heading.style.removeProperty("background");
heading.style.removeProperty("padding");
button.style.removeProperty("background");
para.style.removeProperty("fontWeight");
}

Output

輸出量

remove CSS property using JavaScript (5)

Everything should remain the same as we have not yet called or invoked our function so let's do it now inside our console,

一切都應該保持不變,因為我們尚未調用或調用函數,因此現在讓我們在控制臺中進行操作,

    removeProperties();

Output

輸出量

remove CSS property using JavaScript (6)

Voila! We have successfully removed all our CSS properties using JavaScript! Can you check for yourself is this method works for inline styles?

瞧! 我們已經使用JavaScript成功刪除了所有CSS屬性 ! 您可以自己檢查一下這種方法是否適用于內聯樣式?

翻譯自: https://www.includehelp.com/code-snippets/how-to-remove-css-property-using-javascript.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541921.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541921.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541921.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Django 緩存系統

Django 是動態網站&#xff0c;一般來說需要實時地生成訪問的網頁&#xff0c;展示給訪問者&#xff0c;這樣&#xff0c;內容可以隨時變化&#xff0c;但是從數據庫讀多次把所需要的數據取出來&#xff0c;要比從內存或者硬盤等一次讀出來 付出的成本大很多。 緩存系統工作原理…

java web截屏_java_WebDriver中實現對特定的Web區域截圖方法,用過 WebDriver 的同學都知道,We - phpStudy...

WebDriver中實現對特定的Web區域截圖方法用過 WebDriver 的同學都知道&#xff0c;WebDriver 可以對瀏覽器中的頁面進行截圖。例如&#xff1a;public byte[] takeScreenshot() throws IOException {TakesScreenshot takesScreenshot (TakesScreenshot) driver;return takesSc…

c語言 關鍵字const_C ++ const關鍵字| 查找輸出程序| 套裝1

c語言 關鍵字constProgram 1: 程序1&#xff1a; #include <iostream>using namespace std;void fun(int& A) const{A 10;}int main(){int X 0;fun(X);cout << X;return 0;}Output: 輸出&#xff1a; [Error] non-member function void fun(int) cannot ha…

【喜報】JEEWX榮獲“2016 年度碼云新增熱門開源軟件排行榜”第一名!

為什么80%的碼農都做不了架構師&#xff1f;>>> 2016 年度碼云新增項目排行榜 TOP 50 正式出爐&#xff01;根據 2016 年在碼云上新增開源項目的 Watch、Star、Fork 數量以及其他角度的統計&#xff0c;JEEWX捷微管家榮獲“2016 年度碼云新增熱門開源軟件排行榜”第…

java 二叉樹特點_瘋狂java筆記之樹和二叉樹

樹的概述樹是一種非常常用的數據結構&#xff0c;樹與前面介紹的線性表&#xff0c;棧&#xff0c;隊列等線性結構不同&#xff0c;樹是一種非線性結構1.樹的定義和基本術語計算機世界里的樹&#xff0c;是從自然界中實際的樹抽象而來的&#xff0c;它指的是N個有父子關系的節點…

編輯距離 dp_使用動態編程(DP)編輯距離

編輯距離 dpProblem: You are given two strings s1 and s2 of length M and N respectively. You can perform following operations on the string. 問題&#xff1a;給您兩個長度分別為M和N的字符串s1和s2 。 您可以對字符串執行以下操作。 Insert a character at any posi…

tomcat +apache 配置集群

2019獨角獸企業重金招聘Python工程師標準>>> APACHE2.2.25TOMCAT6.0.37配置負載均衡 目標: 使用 apache 和 tomcat 配置一個可以應用的 web 網站&#xff0c;要達到以下要求&#xff1a; 1. Apache 做為 HttpServer &#xff0c;后面連接多個 tomcat 應用實例&…

java雙緩存機制_詳解JVM類加載機制及類緩存問題的處理方法

前言大家應該都知道&#xff0c;當一個Java項目啟動的時候&#xff0c;JVM會找到main方法&#xff0c;根據對象之間的調用來對class文件和所引用的jar包中的class文件進行加載(其步驟分為加載、驗證、準備、解析、初始化、使用和卸載)&#xff0c;方法區中開辟內存來存儲類的運…

oracle中dbms_并發和由于DBMS中的并發導致的問題

oracle中dbms并發 (Concurrency) The ability of a database system which handles simultaneously or a number of transactions by interleaving parts of the actions or the overlapping this is called concurrency of the system. 數據庫系統通過交織部分操作或重疊操作來…

什么是mvc?

什么是MVCMVC 是一種設計模式&#xff0c;它將應用劃分為3 個部分&#xff1a;數據&#xff08;模型&#xff09;、展現層&#xff08;視圖&#xff09;和用戶交互層&#xff08;控制器&#xff09;。換句話說&#xff0c;一個事件的發生是這樣的過程&#xff1a;1&#xff0e;…

mysql的安裝和基本命令_MySQL安裝以及簡單命令用法

MYSQL:關系型數據庫存儲引擎:負責將邏輯層的概念轉化為物理層機制&#xff0c;在物理層完成物理機制。支持事務&#xff1a;transaction必須滿足的條件&#xff1a;ACID(一致性,持久性,原子性,隔離性)鎖&#xff1a;并發訪問隨機訪問&#xff1a;數據在磁盤上是隨機存儲的安裝&…

將數組轉換為JavaScript中的對象

Lets say you have the following array, 假設您有以下數組&#xff0c; const nums [1, 2, 3, 4, 5];console.log(nums);Output 輸出量 (5) [1, 2, 3, 4, 5]We know that nums is an array and we can see in the output that we get an array. Lets convert it into an ob…

docker集群運行在calico網絡上

2019獨角獸企業重金招聘Python工程師標準>>> ##網絡及版本信息 docker1 centos7 192.168.75.200 docker2 centos7 192.168.75.201 物理網絡 192.168.75.1/24 Docker version 1.10.3, build 3999ccb-unsupported &#xff0c;安裝過程略 # calicoctl version Version…

python批量雷達圖_python批量制作雷達圖

老板要畫雷達圖&#xff0c;但是數據好多組怎么辦&#xff1f;不能一個一個點excel去畫吧&#xff0c;那么可以利用python進行批量制作&#xff0c;得到樣式如下&#xff1a;首先制作一個演示的excel&#xff0c;評分為excel隨機數生成&#xff1a;1 INT((RAND()4)*10)/10加入標…

JavaScript中帶有示例的Math.log()方法

JavaScript | Math.log()方法 (JavaScript | Math.log() Method) Math.log() is a function in math library of JavaScript that is used to return the value of natural Log i.e. (base e) of the given number. It is also known as ln(x) in mathematical terms. Math.log…

SUI踩坑記錄

SUI踩坑記錄 最近做了個項目選型了SUI和vue做單頁應用。下面記錄一下踩坑經歷SUI 介紹 sui文檔&#xff1a;http://m.sui.taobao.org/SUI Mobile 是一套基于 Framework7 開發的UI庫。它非常輕量、精美&#xff0c;只需要引入我們的CDN文件就可以使用&#xff0c;并且能兼容到 i…

java 寫入xml文件_java讀寫xml文件

要讀的xml文件李華姓名>14年齡>學生>張三姓名>16年齡>學生>學生花名冊>package xml;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.Iterator;import java.util.Vector;import javax.xml.pa…

JavaScript中帶有示例的Math.max()方法

JavaScript | Math.max()方法 (JavaScript | Math.max() Method) Math.max() is a function in math library of JavaScript that is used to return the greatest value of all the passed values to the method. Math.max()是JavaScript數學庫中的函數&#xff0c;用于將所有…

java 修飾符默認_Java和C#默認訪問修飾符

C#中&#xff1a;針對下面幾種類型內部成員的訪問修飾符&#xff1a;enum的默認訪問修飾符&#xff1a;public。class的默認為private。interface默認為public。struct默認為private。其中&#xff1a;public可以被任意存取&#xff1b;protected只可以被本類和其繼承子類存取&…

JavaScript中帶有示例的Math.abs()方法

JavaScript | Math.abs()方法 (JavaScript | Math.abs() Method) Math operations in JavaScript are handled using functions of math library in JavaScript. In this tutorial on Math.abs() method, we will learn about the abs() method and its working with examples.…