最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent Enter key works as mouse click - Stack Overflow

programmeradmin3浏览0评论

I noticed that if you focus on an element that mouse clic can be triggered, the Enter keys acts like as you left click the mouse. I want to avoid this running since it es into conflict in other pieces of my code.

In the following example if I focus on this imageButton and I clic once, the next clicks can be "done" with the Enter key, so I don't want this because this button fires a slideToggle() and shows a hidden div, so IMO it's pointless toggle this div with the keyboard.

Is there any way to make it global way? Thank you.

I noticed that if you focus on an element that mouse clic can be triggered, the Enter keys acts like as you left click the mouse. I want to avoid this running since it es into conflict in other pieces of my code.

In the following example if I focus on this imageButton and I clic once, the next clicks can be "done" with the Enter key, so I don't want this because this button fires a slideToggle() and shows a hidden div, so IMO it's pointless toggle this div with the keyboard.

Is there any way to make it global way? Thank you.

Share Improve this question edited Nov 22, 2012 at 13:31 anmarti asked Nov 22, 2012 at 13:18 anmartianmarti 5,14310 gold badges60 silver badges96 bronze badges 3
  • 1 Listen for "keypress" and .preventDefault() – Paul S. Commented Nov 22, 2012 at 13:19
  • @PaulS. this should be an answer I think – Kos Commented Nov 22, 2012 at 13:20
  • 1 Preventing this action may be frustrating for the users of this application, since it is a mon behavior. I suggest you find an alternative system to your buttons. (This is just a suggestion). – Jeff Noel Commented Nov 22, 2012 at 13:27
Add a ment  | 

6 Answers 6

Reset to default 4

To address this you can call preventDefault() on the event that's raised. You can detect the key that was pressed, and if it was the Return key, cancel its default behaviour. Try this:

$(".myElements").on('keypress', e => {
  if (e.key == 'Enter') {
    e.preventDefault();
    console.log('Enter key was pressed');
  }
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<input class="myElements" />

Listen for "keypress" and .preventDefault()

ex. with <myelm class="nokey"/>

function noKeyPressing(){
    var elms = document.getElementsByClassName('nokey'),
        stop = function stop(e){ return e.preventDefault(), false; },
        i = elms.length;
    while(--i >= 0){
        elms[i].addEventListener('keypress', stop, true);
    }
}
noKeyPressing()

If you just want to prevent Enter then the keyCode to look for is 13.

try

.unbind('keydown');

to disable all key events on your element

You can return false to prevent the default action.

  <input type="submit" onkeypress="return false;" value="Submit" />

An other possible way i think:

$('.elems').on('click',function(){$(this).blur()});

try this code

    $('body *').keypress(function (e) {
    if (e.which == 13) {
        e.preventDefault();
    }
});

the above code will prevent pressing enter for every element in page ,You can change the selector $('body *') to something else depending to your case

发布评论

评论列表(0)

  1. 暂无评论