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>

查询

(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 元素的点击。

  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"); 
        });
    });

我的小提琴演示

您也可以将这个想法与这样的 keydown 功能融合在一起......

$(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 回答