CSS 按钮:焦点伪类
我该如何做到这一点,以便当我单击一个按钮时,该按钮保持该颜色,直到单击另一个按钮?
为了澄清这一点,假设您有一个文本框。当您单击它时,您可以添加边框,因为您有像 input:focus{border:#000}
这样的边框,并且当输入失去焦点或单击另一个文本框时,边框属性将返回到默认值。
我如何用一个按钮来完成这个任务。我觉得我需要使用 :after 或其他东西。
我的代码
button.top_bar {
background-color:#E3E3E3;
border:#DCDCDC 1px solid;
border-radius:3px;
height:40px;
display:block;
color:#000;
postion:relative;
display:block;
float:left;
top:5;
text-transform:capitalize;
font-family:Arial;
font-weight:bold;
cursor:pointer;
margin-left:15px;
}
button.top_bar:hover {
border:#9F9F9F 1px solid;
}
button.top_bar:focus {
border:#9F9F9F 1px solid;
}
How Would I make it so that when I click a button, the button stays that color until another button is clicked?
To clarify, imagine you have a text box. When you click it, you can add a border because you have it like input:focus{border:#000}
and when the input loses focus or another text box is clicked, the border properties go back to the default.
How would I accomplish this with a button. I feel like I'd need to use :after or something.
My Code
button.top_bar {
background-color:#E3E3E3;
border:#DCDCDC 1px solid;
border-radius:3px;
height:40px;
display:block;
color:#000;
postion:relative;
display:block;
float:left;
top:5;
text-transform:capitalize;
font-family:Arial;
font-weight:bold;
cursor:pointer;
margin-left:15px;
}
button.top_bar:hover {
border:#9F9F9F 1px solid;
}
button.top_bar:focus {
border:#9F9F9F 1px solid;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的唯一方法是使用 jQuery 或某种 Javascript。我将这样做:我将通过一个类来控制它(我们称之为“.selectedBorder”)。然后单击时,抓住所有按钮,并关闭所有按钮的边框,然后将其添加到单击的按钮上。这是一个例子:
这应该可以解决问题。只需将其添加到 javascript 标记或外部文件中并包含它,您就可以开始了。希望有帮助。
The only way I can think of doing this is with jQuery, or some sort of Javascript. Here's how I would do it: I would control it via a class (let's call it ".selectedBorder"). Then on click, grab all your buttons that you have, and turn off the borders for all of them, then just add it on the clicked one. Here's an example:
That should do the trick. Just add that in a javascript tag or an external file and include it, and you should be good to go. Hope that helps.
与文本输入不同,某些浏览器在单击按钮元素时似乎不会授予焦点。在按钮元素中添加一个属性 onclick="this.focus()" 来强制解决该问题。不需要 jQuery :-)
Unlike text inputs, some browsers don't seem to grant focus when button elements are clicked. Add to your button element an attribute onclick="this.focus()" to force the issue. No jQuery needed :-)