2

请看下面的代码:

$('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当元素隐藏时如何禁用?

4

2 回答 2

1

我不认为有办法禁用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>

于 2016-10-25T06:04:25.337 回答
1

您可以检查是否divhidden事件click中:

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

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

});

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

于 2016-10-25T06:05:43.387 回答