admin管理员组文章数量:1332404
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
6 Answers
Reset to default 4To 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
本文标签: javascriptPrevent Enter key works as mouse clickStack Overflow
版权声明:本文标题:javascript - Prevent Enter key works as mouse click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742275957a2445200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论