作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在鼠标单击时从按钮上移除焦点但在使用 Javascript 按下 Enter
键时不移除焦点的最佳方法是什么?我在 click
事件上使用 blur()
方法,但这不起作用。
请注意,出于可访问性原因,我无法使用 outline: none
或 outline: 0
。欲了解更多信息,请参阅:http://www.outlinenone.com/
var button = document.getElementById("btn");
button.addEventListener("click", removeFocus);
function removeFocus(event) {
event.target.blur();
}
button {
display: inline-block;
border: none;
height: 36px;
line-height:36px;
padding: 0 12px;
background: cyan;
}
<button id="btn">
Hello
</button>
最佳答案
Javascript解决方案
var button = document.getElementById("btn");
button.addEventListener("click", clickEvent);
button.addEventListener("keypress", clickEvent);
function clickEvent(event){
if (event.keyCode == 13) {
event.preventDefault();
} else {
button.blur();
}
}
button {
display: inline-block;
border: none;
height: 36px;
line-height:36px;
padding: 0 12px;
background: cyan;
}
<button id="btn">
Hello
</button>
关于javascript - 自定义按钮移除单击时的焦点,但不移除 Enter 按键的焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39841556/
我正在做一个项目,我的 android 在这个项目中作为一个网络服务器工作;输入带端口号的 IP 地址,打开 Web 界面,用户可以将文件上传到手机。我想在 Web 界面上显示一些图片,以便我们的界面
我是一名优秀的程序员,十分优秀!