好家伙即时通讯建立一个库,比如我举一个属性附加伤害到html元素触发事件不会工作
<a href="https://google.com" shc="27">
logout
像这样的SHC =“27”,这意味着当钥匙27(ESC )将被点击在键盘上,它会触发点击链接,但由于某种原因,它拒绝点击它。 这里是我的全码:
$(document).on("keydown", function(e) {
console.log(e.which);
checkElementType(e.which);
});
function checkElementType(shortcutKey){
var element = $("[shc="+shortcutKey+"]");
var elementType = element.prop('nodeName');
switch(elementType){
case 'A':
console.log(element);
console.log('click the link');
element.trigger('click');
break;
}
}
这里是小提琴:Fiddle
不相关的问题,但你应该使用['数据 - *'](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data- *)属性而不是自定义属性。 – Andreas
[jQuery的如何触发href元素上的点击事件]的可能重复(http://stackoverflow.com/questions/7999806/jquery-how-to-trigger-click-event-on-href-element) – Andreas
@Andreas为什么使用数据属性而不是自定义属性? – Jazz