3

我需要让我的按钮在:active按下某个键盘按钮时变为状态,而不仅仅是在单击时。

这是我的代码...在此先感谢。

脚本 :

$(document).on('keypress', function (e) {
    var tag = e.target.tagName.toLowerCase();
    if (e.which === 119 && tag != 'input' && tag != 'textarea')
        $('#forward').click();
    if (e.which === 115 && tag != 'input' && tag != 'textarea')
        $('#back').click();
    if (e.which === 97 && tag != 'input' && tag != 'textarea')
        $('#left').click();
    if (e.which === 100 && tag != 'input' && tag != 'textarea')
        $('#right').click();
});

$(function () {
    $("#forward").click(function () {
        $.ajax('/forward');
    });
    $("#back").click(function () {
        $.ajax('/back');
    });
    $("#left").click(function () {
        $.ajax('/left');
    });
    $("#right").click(function () {
        $.ajax('/right');
    });
});

HTML:

<ul class="button-list">
 <li>
    <button id="forward">W</button>
 </li>
 <li>
    <button id="back">S</button>
 </li>
 <li>
    <button id="left">A</button>
 </li>
 <li>
    <button id="right">D</button>
 </li>
</ul>

..................................................... ...................................

4

4 回答 4

7

您必须创建与 soclass的状态style相同的a ::activebutton

一般示例

.buttonclass:active , .activated { /* somestyle */ }

$('.buttonclass').on('click', function(){ 
  $(this).addClass('activated'); //eventually removeClass of some previous class
  //other stuff
});

您的代码

CSS

#forward:active, .forwardactivate{}
#back:active, .backactivate{}

或者,如果您有,ul li button style那么您必须这样做:

ul li button:active, .activatebutton {} /* style*/

JAVASCRIPT

     $(function() {
            $("#forward").click(function() {
              $.ajax('/forward');
              if($(this).hasClass('forwardactivate'))
                $(this).removeClass('forwardactivate'); //change with .activatebutton
              else      
               $(this).addClass('forwardactivate');
             });

            //etc
     });
于 2013-08-28T09:40:37.247 回答
3

$(document).ready(function () {
$("ul > li button").click(function () {
        $('ul > li button').removeClass('active');
        $(this).addClass('active');
    });
});
ul > li button.active{
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="button-list">
 <li>
    <button id="forward">W</button>
 </li>
 <li>
    <button id="back">S</button>
 </li>
 <li>
    <button id="left">A</button>
 </li>
 <li>
    <button id="right">D</button>
 </li>
</ul>

于 2018-10-02T04:31:56.463 回答
0

我不知道您对“将按钮设置为 .Active”是什么意思,但例如您可以通过以下代码触发它的点击:

$(document).keypress(function(e){

    if(e.keychar == 'w')
    {
        $("#forward").trigger('click');
        //You can do any thing here ex. "set button state Active"
        //$("#forward").addClass('active');
    }
    else if(e.keychar == 's')
        $("#back").trigger('click');
    else if(e.keychar == 'a')
        $("#left").trigger('click');
    else if(e.keychar == 'd')
        $("#right").trigger('click');

});
于 2013-08-28T09:39:33.517 回答
0

can't you add .active class according to the page... say if you are on /forward add .active to forward... or you can do it like this too:

$("#forward").click(function() {
      $.ajax('/forward');
      $('button').removeClass('active');
      $(this).addClass('active');
});
于 2013-08-28T09:42:19.190 回答