【问题标题】:Set button to active on click?将按钮设置为单击时激活?
【发布时间】:2013-07-14 18:27:11
【问题描述】:

我试图在单击按钮时更改按钮的颜色,但它不起作用。 .radio_button:hover 函数有效,但 .radio_button:active 无效。 (我认为 CSS 被引导 CSS 覆盖,可能是这种情况(如果是这样,我该如何覆盖引导 css)?

带按钮的表单:

<table border="0" width="100%" cellpadding="5px">
  <tr>
    <td>
      <%= simple_form_for(@post, :html => {:class => 'form-inline' }) do |f| %>
      <%= f.input :link, label: false %>
      <%= f.input :tag_list, placeholder: "Add some tags...", label: false %>
      <div id="type_group" class="btn-group" data-toggle="buttons-radio">
        <%= f.input :type, as: :hidden %>
          <a class="radio_button" id="radio_button_zero" data-value="0">ZERO</a>
          <a class="radio_button" id="radio_button_one" data-value="1">ONE</a>
          <a class="radio_button" id="radio_button_two" data-value="2">TWO</a>
      </div>
      <%= button_tag(type: 'submit', class: "submit_button") do %>
        POST!
      <% end %>
      <% end %>
    </td>
  </tr>
</table>

CSS:

.radio_button {
        color: white;
        background-color:#828282;
        font-family: 'Fjalla One', serif;
        font-size: 10pt;
        font-weight: 100;
        text-align: center;
        font-style: italic;
        padding:0px 6px;
        padding-right: 8px;
        padding-top: 3px;
        padding-bottom: 2px;
        text-decoration:none;
        border: none;
    }.radio_button:hover {
        background-color:#0E9EBB;
        color: white;
        text-decoration:none;
    }.radio_button:active {
        position:relative;
        background-color:#0E9EBB;
        top:1px;
    }.radio_button,.btn:focus{
        outline:none!important
    }

咖啡脚本:

$ ->
  $("a.radio_button").click (e) ->
    e.preventDefault();
    clickedValue = $(this).data("value")
    $("#post_type").val clickedValue
    if clickedValue == 0
      $("#radio_button_zero").addClass('active')

【问题讨论】:

  • 你能在 jsfiddle 中重现你的问题吗?
  • @raam86,相信这是咖啡脚本
  • @vinodadhikary 好东西

标签: javascript css twitter-bootstrap coffeescript


【解决方案1】:

【讨论】:

  • 我可能遗漏了一些东西,但这看起来不像是我的答案。
  • 根据您的 jquery 功能,它会在单击第一个按钮时激活按钮
【解决方案2】:

如果您想覆盖引导 css,只需在引导程序之前包含您的 css ...或者您可以使用 jQuery ...

<style>
   .active {
      position:relative;  
      background-color:#0E9EBB;  /* u can use background-color:#0E9EBB!important;*/
      top:1px;
</style>

$('button').click(function (){
  $(this).addClass('active');
});

【讨论】:

  • 没有办法禁用引导 CSS 吗? (因为我的 CSS 在 .css 文件中,而不是在与 HTML 相同的文件中)。
  • 好的,然后像这样包含css: 并为按钮使用一些“更强”的选择器,如 id 或一些精确的类
猜你喜欢
  • 1970-01-01
  • 2011-05-01
  • 2021-02-03
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
相关资源
最近更新 更多