法1:
使用CSS removeProperty:該CSSStyleDeclaration.removeProperty()方法被用來從一個元件的樣式刪除一個屬性。
通過遍歷styleSheets數組并選擇cssRule,可以選擇元素的樣式。然后可以使用要刪除的屬性指定removeProperty方法。
句法:
element.removeProperty('property')
實例1:
<!DOCTYPE html> <html> <head> <title> How to remove CSS property using JavaScript? </title> <style> .elem { color: green; font-size: 3rem; text-decoration: underline; } </style> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to remove CSS property using JavaScript? </b> <div class="elem">Hello World!</div> <p> Click on the button below to remove the text decoration of the element </p> <button onclick="removeProperty()"> Remove text-decoration property </button> <script> function removeProperty() { element = document.styleSheets[0].cssRules[0].style; element.removeProperty('text-decoration'); } </script> </body> </html>
輸出:
在單擊按鈕之前:
單擊按鈕后:
方法2:使用setProperty方法:該CSSStyleDeclaration.setProperty()方法可用于設置的樣式的所需的屬性。選擇必須刪除其屬性的元素,并將此屬性應用于其style屬性。將此屬性設置為“ initial”可將屬性重置為其初始值,從而消除該屬性的任何影響。
句法:
element.style.setProperty('color','initial')
例如:
<!DOCTYPE html> <html> <head> <title> How to remove CSS property using JavaScript? </title> <style> .elem { color: green; font-size: 3rem; text-decoration: underline; } </style> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to remove CSS property using JavaScript? </b> <div class="elem">Hello World!</div> <p> Click on the button below to remove the text color of the element </p> <button onclick="removeProperty()"> Remove color property </button> <script> function removeProperty() { element = document.querySelector('.elem'); element.style.setProperty('color', 'initial'); } </script> </body> </html>
輸出:
單擊按鈕后:
本文完~
每日更新一個開發(fā)小技巧,希望對你有幫助哦,覺得有用的麻煩點點贊支持一下哦,謝謝~~
SS(級聯樣式表)是網頁設計的支柱,用于設計網站的視覺呈現。雖然您可能已經熟悉許多 CSS 屬性,但存在一些較少討論的屬性,它們可以增強您的樣式功能。在這篇文章中,我將向您介紹 15 個帶有代碼片段的 CSS 屬性。
當涉及到復選框和單選按鈕等輸入時,瀏覽器通常會使用默認顏色,該顏色可能與 UI 的選定配色方案不太協調。
若要保持 UI 的一致性,可以使用 accent-color 屬性更改輸入的默認顏色。
例如:
SS(層疊樣式表)是一種用于描述網頁文檔外觀和格式的標記語言。以下是一些常用的CSS屬性和樣式:
*請認真填寫需求信息,我們會在24小時內與您取得聯系。