隐藏元素时如何禁用“accesskey”?

How to disable `accesskey` when the element is hidden?

请看下面的代码:

$('div').click(function(){
   alert("Shortcut worked although element was hidden!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div hidden accesskey="1">
    Some things...
</div>

<p>
  Focus on me and then 
  </br>
  <b>press [Alt + 1] please!</b>
</p>

如何在元素隐藏时禁用 accesskey

我认为没有办法禁用 accesskey,您只需要 add / remove 根据您的需要

if(!$('div').is(":visible"))
{
   $('div').removeAttr('accesskey');
}
else
{
   $('div').attr('accesskey', '1');
}

if(!$('div').is(":visible"))
{
  $('div').removeAttr('accesskey');
}
else
{
  $('div').attr('accesskey', '1');
}
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div hidden accesskey="1">
    Some things...
</div>

<p>
  Focus on me and then 
  </br>
  <b>press [Alt + 1] please!</b>
</p>

您可以在click事件中检查div是否为hidden

$('div').click(function(event) {

  if (!$(this).is(':hidden')) {
    alert("Shortcut worked although element was hidden!");
  } else {
    event.preventDefault();
  }

});

https://jsfiddle.net/89b2jtmw/1/