开发者

Remove DOM object on click and buttonpress

开发者 https://www.devze.com 2023-01-29 18:57 出处:网络
Pretty sure i\'m going the complete wrong direction with this. I want to click an object, then delete it by pressing the \'d\' key.

Pretty sure i'm going the complete wrong direction with this. I want to click an object, then delete it by pressing the 'd' key.

<script>
$('#In_Play .card').click().keypress(function(event) {
  if (event.keyCode == '100') {
    $(this).re开发者_StackOverflowmove();
    }
});
</script>


I think something like this would work:

$('#In_Play .card').click(function(){
   $(this).data('delcandidate', true).focus();
}).keypress(function(event){
    if($(this).data('delcandidate') == true && event.keyCode == '100'){
      $(this).remove();
    }
}).blur(function(){
   $(this).data('delcandidate', false);
});

the thing is keypress will only be fired on elements that can have focus (inputs, a). I think you can extend this to other elements by adding a tabindex but im not sure how cross browser any of this is going to be.

0

精彩评论

暂无评论...
验证码 换一张
取 消