html - 如何制作:active state work in IE?

标签 html css internet-explorer button

我的 html 表单中有一个按钮,需要在使用 css 单击它时更改它的背景图像。它在 FF 中运行完美,但似乎 IE 不支持 :active 状态。

这是我的代码:

HTML:

<button class='button'>Click Me</button>

CSS:

.button {
    width: 118px;
    height: 33px;
    background: url(/images/admin/btn.png) no-repeat center top;
    border: none;
    outline: none;
}
.button:active {
    background-position: center bottom;
}

最佳答案

这是 IE 早期版本中的一个已知错误(我认为他们在 IE8 中解决了它)。我通常用 javascript 解决这个问题(以及相应的“悬停”问题)。我将两个事件处理程序附加到该元素——“mousedown”以设置一个附加类(类似于“button-active”)和“mouseup”以删除该类。在 jQuery 中它会是这样的:

$('.button').mousedown(function() { $(this).addClass('button-active'); });
$('.button').mouseup(function() { $(this).removeClass('button-active'); });

然后,只需将该类添加到 css 规则中,如下所示:

.button:active, .button-active {
    background-position: center bottom;
}

是的,有点难看,但您还能期待什么——它是 Internet Explorer。它不可能很漂亮。

关于html - 如何制作:active state work in IE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4057861/

相关文章:

html - DIV 覆盖整个页面(IE7)

javascript - 循环ajax调用来填充多个div

javascript - 同步 CSS 动画

html - 使用 xsl :element tag for html transformation

javascript - 如何根据下拉选择显示和隐藏表单中的字段?

javascript - 无法在IE 11中打印iframe

javascript - jQuery 1.3.2 无法在 Internet Explorer 11 上运行

html - 对 input[type=submit] 应用某种效果

javascript - Css 动画 - setTimeout 不应用会破坏动画的更改

html - 半透明图像边框