2

我有以下 jQuery 代码,它只作为类工作,但:hover导致它不起作用。

$('.dd_question:hover').css('border','1px dashed #333333');

我可以在$()方法中使用一些特殊的代码吗?还是:hover根本不允许?

4

2 回答 2

6

The jQuery selector selects elements that match the selector when the code is executed, not when the user hovers over an element.

With that in mind, $('.dd_question:hover') will only select elements that have that class and that were currently being hovered over when the code was executed which most likely was 0 elements.

You need to either use the mouseenter and mouseleave events (or hover for short), or simply use css.

于 2013-05-20T17:51:49.323 回答
6

其他人已经解释了为什么您使用的代码不起作用,但另一个问题是如何修复它。获得您想要的效果的最佳方法可能是拥有

.hoverborder:hover {border: 1px dashed #333333;}

在你的CSS中,然后使用

$('.dd_question').addClass('hoverborder');

在你的 JavaScript 中。

于 2013-05-20T17:53:40.413 回答