问题
I tried preventDefault()
but I haven't had success. Is there something that I'm missing?
I would try to disable it globally, if possible (like registering the events on window
)
回答1:
There doesn't seem to be a way to stop the event from triggering. The only alternative seems to be to remove the accesskey attributes temporarily while you don't want them to work. That's what jQuery UI has to do for modal dialogs.
Here's the code from that thread:
$("#boxA-dialog").dialog({
autoOpen: false,
modal: true,
height: 400,
width: 300,
open: function(event, ui) {
ak = $('[accesskey]').each(function() {
$(this).data('ak', $(this).attr('accesskey')).removeAttr('accesskey')
})
},
close: function(event, ui) {
ak.each(function() {
$(this).attr('accesskey', $(this).data('ak'))
})
}
});
As you can see it's saving the accesskey attributes to jQuery data before removing them:
$(this).data('ak', $(this).attr('accesskey')).removeAttr('accesskey')
and then restoring them from the data:
$(this).attr('accesskey', $(this).data('ak'))
I'd be interested in a solution that actually prevents the event rather than using this workaround.
回答2:
If I correctly understand your problem, you could try this.
$('[accesskey*=]').focus(function(e) {
// disable accesskey functionality
});
$('[accesskey*=]').blur(function(e) {
// reenable accesskey functionality
});
回答3:
Using the previous answer, you can use this trick to "remove" all "accesskey" options from the page.
$('[accesskey*=]').attr('accesskey','');
Simply set all accesskey on the page for nothing.
来源:https://stackoverflow.com/questions/22670735/is-there-a-way-to-prevent-htmls-accesskey-from-being-activated