javascript - 如何使用 :hover stay in hover state on click? 制作一个 div

标签 javascript jquery html css hover

我不确定如何问这个问题,但基本上,我有一个带有按钮的 div,在 CSS 中有一些 :hover 状态,但是,我希望在单击按钮时,它会保持悬停状态,这可能吗?

我的代码

#englishbutton {
    width:200px;
    height:50px;
    font-family: 'Raleway', sans-serif;
    margin:0px 20px 20px 20px;
    border:none;
    outline:none;
    background-color:#F16D71;
    color:#FFFFFF;
    transition: .3s background-color;
    text-align:center;
    font-size:20px;
    border-radius:15px;
}

#englishbutton:hover {
    background-color:#F5999C;
    color:#FFFFFF;
    cursor:pointer;
}
<div id="actionsection">
        <input name="englishbutton" type="button" id="englishbutton" value="ENGLISH">
</div>

我曾尝试搜索类似的问题,其中一个说使用 :focus state 但它不起作用,任何帮助都将不胜感激!

最佳答案

有很多方法可以做到这一点:

你可以使用:focus:

#englishbutton {
    width:200px;
    height:50px;
    font-family: 'Raleway', sans-serif;
    margin:0px 20px 20px 20px;
    border:none;
    outline:none;
    background-color:#F16D71;
    color:#FFFFFF;
    transition: .3s background-color;
    text-align:center;
    font-size:20px;
    border-radius:15px;
}

#englishbutton:hover, #englishbutton:focus {
    background-color:#F5999C;
    color:#FFFFFF;
    cursor:pointer;
}
<div id="actionsection">
  <input name="englishbutton" type="button" id="englishbutton" value="ENGLISH">
</div>

使用 jQuery,您可以使用 css() 在按钮的 click 上添加背景方法:

$('#englishbutton').on('click', function(){
  $(this).css('background', '#F5999C');
});
#englishbutton {
    width:200px;
    height:50px;
    font-family: 'Raleway', sans-serif;
    margin:0px 20px 20px 20px;
    border:none;
    outline:none;
    background-color:#F16D71;
    color:#FFFFFF;
    transition: .3s background-color;
    text-align:center;
    font-size:20px;
    border-radius:15px;
}

#englishbutton:hover {
    background-color:#F5999C;
    color:#FFFFFF;
    cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="actionsection">
  <input name="englishbutton" type="button" id="englishbutton" value="ENGLISH">
</div>

或者您可以使用 addClass() 添加样式化事件类的方法如下:

$('#englishbutton').on('click', function() {
  $(this).addClass('active');
});
#englishbutton {
  width: 200px;
  height: 50px;
  font-family: 'Raleway', sans-serif;
  margin: 0px 20px 20px 20px;
  border: none;
  outline: none;
  background-color: #F16D71;
  color: #FFFFFF;
  transition: .3s background-color;
  text-align: center;
  font-size: 20px;
  border-radius: 15px;
}
#englishbutton:hover, #englishbutton.active {
  background-color: #F5999C;
  color: #FFFFFF;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="actionsection">
  <input name="englishbutton" type="button" id="englishbutton" value="ENGLISH">
</div>

关于javascript - 如何使用 :hover stay in hover state on click? 制作一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41959779/

相关文章:

javascript - 无法在 Google map 脚本标记中使用回调

javascript - 从 AJAX GET 获取 HTML 值

JavaScript 淡入/淡出 Div

c# - 捕获 C# 中的 JavaScript 错误

javascript - TypeError : socket. 发出不是一个函数

javascript - 使用数组对对象进行排序并将值存储在另一个数组中

javascript - iframe 以外的 javascript 变量名冲突的解决方案?

javascript - Meteor 的 createUser 在客户端和服务器上运行

Javascript | HTML Canvas 错误

html - 如何在 float 上堆叠 div?