JavaScript - 保留伪类(:hover, :active, :focus) active, 直到发布

标签 javascript css class triggers release

我如何才能保持这些状态(:hover、:focus、:active 等)处于事件状态,直到被释放?

看看这个例子:

<style> div:hover, div:focus, div:active { background: red; } </style>

<div tabindex="-1">I would be red, if hovered, focused or being active. Until the button 'toggle off' is pressed.</div>

<button onclick="toggleItem('on', 'hover');">Toggle on</button>
<button onclick="toggleItem('off', 'hover');">Toggle off</button>

function toggleItem(action, style)
{
    if (action == "on")
    {
        // style activation
    }
    else if (action == "off")
    {
        // disable activated style
    }
}

我想设置一个时间间隔,每隔x秒触发一次这个状态。但似乎不是“好的做法”……有什么建议吗?

基本上就像开发者工具一样:

enter image description here

最佳答案

为什么你特别需要那些状态?

您可以向您的 div 添加类:.div_hover, .div_focus{ 您的样式 } 并在您的 JS 脚本中添加/删除这些类。

关于JavaScript - 保留伪类(:hover, :active, :focus) active, 直到发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971544/

相关文章:

Javascript如何遍历对象以拾取选定的项目

css - Visual Studio 查找 CSS 类的用法

html - 3 边的 CSS 阴影

c++ - 从对象调用成员对象,错误: initial value of reference to non-const must be an lvalue

c# - 如何使用 List.Distinct 来填充自定义类?

java - 尝试更改类名时出错

javascript - 是否可以让 chrome 片段自动运行?

javascript - 在按钮内渲染 SVG 图标?

javascript - 检测动态页面滚动到底部

css - 媒体查询不适用于 Blogspot