LoginSignup
5
4

More than 5 years have passed since last update.

JavaScriptから :visited 用のCSSルールを追加する

Posted at

訪問済みリンクを見やすくするためにGreasemonkeyスクリプトを書いていて嵌ったのでメモ。

通常のCSSの場合

通常のCSSルールを追加する場合は以下のように書ける。

var sheet = document.styleSheets[0];
sheet.insertRule('span {color: #FFF;}', 0);

:visitedを対象としたCSSの場合

一方 :visited 特有の性質として、通常のリンク(疑似クラス指定無し or :link)に
同じプロパティーが設定されていないと有効にならない、というものがある。
したがって:visitedの表示を変える場合は、以下のように通常リンク用の指定とセットで書く必要がある。

var sheet = document.styleSheets[0];
sheet.insertRule('a:visited {background-color: #DDD;}', 0);
sheet.insertRule('a {background-color: #FFF;}', 0);

参考

How do you add CSS with Javascript? - Stack Overflow
http://stackoverflow.com/questions/707565/how-do-you-add-css-with-javascript

:visited | CSS-Tricks
https://css-tricks.com/almanac/selectors/v/visited/

5
4
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
5
4