2

スパン ボックスがクリックされたときに発生するのと同じことをトリガーするキーボード ショートカットをいくつか作成したいと考えています。IE7で動作する必要があります。どうすればこれを行うことができますか? これが私がこれまでに持っているものです:

HTML

<li id="detail">
  <a href="#" onClick="access(this);">
    <span>Detailed Report</span>
  </a>
</li>
<li id="product">
  <a href="#" onClick="access(this);">
    <span>Product Area Report</span>
  </a>
</li>

Jクエリ

(document).keypress(function(e){
    if (!$(e.target).is('input, textarea')) {
        var code = e.which || e.keyCode;
        switch ( code ) {
            case 13: //enter
                getTable();
                return false;
            default:
                break;
        }
    }
});
4

2 に答える 2

3

あなたの質問の元のバージョンから、あなたが探していた、またはクリックイベントを生成する方法を理解しました.

jQuery を使用して要素のクリックをトリガーするには、2 つの非常によく似た方法があります。

  1. $(element).click()

  2. $(element).trigger('click')

trigger()他の種類のイベントにも同じ機能を使用できます。例えば、

$(element).trigger('submit'); // for a form element
$(element).trigger('dblclick'); // for a double click event

参照 -trigger()ドキュメント

于 2013-03-14T13:38:41.127 に答える
1

HTML:

<li id="detail">
  <a href="#" class="detail-report">
    <span>Detailed Report</span>
  </a>
</li>
<li id="product">
  <a href="#" class="product-report">
    <span>Product Area Report</span>
  </a>
</li>

jQuery:

  $(function(){

        $(".detail-report").click(function(e) {
          e.preventDefault();
          /*trigger the other thing here*/
          alert("clicked on the detail report");
          myevent(); //declare a function somewhere in the scope of your js and call it here :)
        });

        $(".product-report").click(function(e) {
          e.preventDefault();
          /*trigger the other thing here*/
          alert("clicked on the product report"); 
        });
    });

私のフィドルデモ

このアイデアをこのようなキーダウン機能とブレンドすることもできます...

$(document).keydown(function(e) {
switch(e.KeyCode) {
//you just gotta know your key codes and what numbers they are associated to!
case 68: //when you press the (d) key trigger something...
  alert('i used my keyboard to press the d key.')
 myevent(); //you can call that same function that you created earlier, and place it here.
break;
  }
});
于 2013-03-14T13:44:20.257 に答える