is there a way to prevent HTML's accesskey=“”

2019-04-20 18:56发布

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)

3条回答
Deceive 欺骗
2楼-- · 2019-04-20 18:59

If I correctly understand your problem, you could try this.

$('[accesskey*=]').focus(function(e) { 
    // disable accesskey functionality 
});
$('[accesskey*=]').blur(function(e) { 
    // reenable accesskey functionality 
});
查看更多
Explosion°爆炸
3楼-- · 2019-04-20 19:11

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.

查看更多
叼着烟拽天下
4楼-- · 2019-04-20 19:17

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.

查看更多
登录 后发表回答