LoginSignup
4
2

More than 5 years have passed since last update.

JSで、ある要素をhoverしてるようにする

Posted at

リンクの要素など、cssで:hoverでスタイルをつけている。

実際にhoverしなくても:hoverで指定したスタイルに一時的にしたい。
例えば、Aをクリックした時に、Bをhover状態のスタイルにしたい。

結論

無理だった
jsでhoverしていることにする、という動作はそもそも無理なようだ。

なので代わりに、.hoveredというクラスも一緒に定義して、jQueryなどでクラスをあてて実現する。

style.css

.B:hover, .B.hovered {
  background-color: #ccc;
}


main.js

$('.A').on('click', function() {
  $('.B').toggleClass('.hovered');
});


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