CSS 按钮:焦点伪类

发布于 2024-12-11 23:42:10 字数 711 浏览 0 评论 0原文

我该如何做到这一点,以便当我单击一个按钮时,该按钮保持该颜色,直到单击另一个按钮?

为了澄清这一点,假设您有一个文本框。当您单击它时,您可以添加边框,因为您有像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡尘雨 2024-12-18 23:42:10

我能想到的唯一方法是使用 jQuery 或某种 Javascript。我将这样做:我将通过一个类来控制它(我们称之为“.selectedBorder”)。然后单击时,抓住所有按钮,并关闭所有按钮的边框,然后将其添加到单击的按钮上。这是一个例子:

//first you grab the click event for the button(s)
$("#buttons").click(function(){

    //we remove all the borders from all the buttons
    $("#buttons").removeClass("selectedBorder");

    //Now we add the border to the button that's been clicked
    $(this).addClass("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:

//first you grab the click event for the button(s)
$("#buttons").click(function(){

    //we remove all the borders from all the buttons
    $("#buttons").removeClass("selectedBorder");

    //Now we add the border to the button that's been clicked
    $(this).addClass("selectedBorder");

});

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.

瞎闹 2024-12-18 23:42:10

与文本输入不同,某些浏览器在单击按钮元素时似乎不会授予焦点。在按钮元素中添加一个属性 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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文