有什么方法可以阻止HTML的accesskey= " "被激活?

时间:2023-01-20 17:17:05

I tried preventDefault() but I haven't had success. Is there something that I'm missing?

我尝试了preventDefault(),但没有成功。有什么东西是我丢的吗?

I would try to disable it globally, if possible (like registering the events on window)

如果可能的话,我将尝试全局禁用它(比如在窗口上注册事件)

3 个解决方案

#1


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.

似乎没有办法阻止事件触发。唯一的选择似乎是暂时删除accesskey属性,而您不希望它们工作。这就是jQuery UI对模态对话框的作用。

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:

如您所见,它在删除jQuery数据之前将accesskey属性保存到jQuery数据中:

$(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


0  

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


0  

Using the previous answer, you can use this trick to "remove" all "accesskey" options from the page.

使用前面的答案,您可以使用此技巧从页面中“删除”所有“accesskey”选项。

$('[accesskey*=]').attr('accesskey','');

Simply set all accesskey on the page for nothing.

简单地设置页面上的所有accesskey。

#1


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.

似乎没有办法阻止事件触发。唯一的选择似乎是暂时删除accesskey属性,而您不希望它们工作。这就是jQuery UI对模态对话框的作用。

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:

如您所见,它在删除jQuery数据之前将accesskey属性保存到jQuery数据中:

$(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


0  

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


0  

Using the previous answer, you can use this trick to "remove" all "accesskey" options from the page.

使用前面的答案,您可以使用此技巧从页面中“删除”所有“accesskey”选项。

$('[accesskey*=]').attr('accesskey','');

Simply set all accesskey on the page for nothing.

简单地设置页面上的所有accesskey。