简体   繁体   English

如果在 JS 中设置了样式,则 CSS:hover 不适用

[英]CSS :hover don't apply if style is set in JS

first time posting a question so sorry if I make mistakes.第一次发布问题,如果我犯了错误,非常抱歉。

I have the following JS code for creating "notes" with a specified rotation:我有以下 JS 代码用于创建具有指定旋转的“注释”:

newNote.style.transform = rotate(30deg);

it works great but when I try to use this CSS it does not get applied (should reset the rotation):它工作得很好但是当我尝试使用这个 CSS 时它没有被应用(应该重置旋转):

.notes:hover{transform: none;}

so after some more trying I think it could be a bug as styling never apply on hover if they been applied in the JS (is there any fix for this or should it be reported as a bug?)因此,经过更多尝试后,我认为这可能是一个错误,因为如果在 JS 中应用了 hover,则样式永远不会适用于它们(是否有任何修复或应该将其报告为错误?)

Using !important can help you.使用!important可以帮助您。 Example:例子:

.notes:hover{transform: none !important;}

Please remove your js code and use these instead:请删除您的 js 代码并改用以下代码:

.notes { transform: rotate(30deg); }
.notes:hover{transform: none;}

The first line adds the rotation always.第一行总是添加旋转。

If you want to add rotation in specific case, use these styles instead:如果您想在特定情况下添加旋转,请改用这些 styles:

.notes.rotate { transform: rotate(30deg); }
.notes:hover{transform: none;}

In your js, add 'rotate' class to the element you need:在您的 js 中,将 'rotate' class 添加到您需要的元素中:

newNote.classList.add('rotate');

It will add the rotation.它将添加旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM