is there a way to prevent HTML's accesskey=“” from being activated?

折月煮酒 提交于 2019-12-05 01:41:43
Sam Hasler

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.

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

$('[accesskey*=]').focus(function(e) { 
    // disable accesskey functionality 
});
$('[accesskey*=]').blur(function(e) { 
    // reenable accesskey functionality 
});

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!