好傢伙即時通訊建立一個庫,比如我舉一個屬性附加傷害到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