我想制作一些键盘快捷键,我想触发单击span-box时发生的同样的事情。它需要在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>
JQUERY
(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;
}
}
});
答案 0 :(得分:3)
我从您正在查看的问题的原始版本中了解到生成点击事件的方式。
使用jQuery触发元素点击有两种非常相似的方法。
$(element).click()
$(element).trigger('click')
相同的trigger()
函数也可用于其他类型的事件。例如,
$(element).trigger('submit'); // for a form element
$(element).trigger('dblclick'); // for a double click event
trigger()
documentation 答案 1 :(得分: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;
}
});