[英]Can I delete a CSS file with JavaScript?
我正在嘗試使用 1 個 html 和很多 CSS 來創建一個網站,但它們一直相互覆蓋。 如何從 JavaScript 中刪除 HTML 的 CSS 文件? 因為他們不再覆蓋了。
覆蓋CSS? 您可能是說一個 css 文件覆蓋了其他 CSS 文件的樣式。 好的,現在這是一個問題,解決它的正確方法是手動刪除罪魁禍首樣式或 css 文件。
您無法通過瀏覽器上的 Javascript 刪除 CSS 文件,因為這是一種客戶端腳本語言。
您可以刪除應用於 html 元素的 css 樣式,是的; 但刪除CSS文件? 不,那是做不到的。
我會說,重新專注於清理網站。
您可以通過 JavaScript 替換或刪除該 CSS 文件,如下所示:
// replace
document.querySelector("link[rel='styleSheet']").setAttribute("href", "anotherStyle.css")
// remove
document.querySelector("link[rel='styleSheet']").removeAttribute("href")
您還可以在使用href
屬性而不是rel
選擇link
標簽時更具體,如下所示:
// replace
document.querySelector("link[href='style.css']").setAttribute("href", "anotherStyle.css")
// remove
document.querySelector("link[href='style.css']").removeAttribute("href")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.