1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptでCSSファイルを切り替える

Last updated at Posted at 2020-11-28

JavaScriptを利用して、読み込むCSSファイルを切り替えます。

##コード

HTMLにはlinkタグを全て記述しておきます。

<link rel="stylesheet" href="1.css" title="a">
<link rel="stylesheet" href="2.css" title="b" disabled>
<link rel="stylesheet" href="3.css" title="c" disabled>

title属性に名前を付けておきます。この名前で切り替えます。
最初に無効化しておきたいファイルにはdisabled属性を付けておく。

function change_css(name){
    for(const css of document.querySelectorAll('link[title]')){
        css.disabled = css.title !== name
    }
}

この関数に読み込みたいファイルのタイトル(a,b,c)を渡せば、ファイルが切り替わります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?