[英]Prevent line break in textarea with JavaScript - how to?
When i click on "enter" I call a function. 当我单击“输入”时,我调用一个函数。 Within this function I do the following: 在此函数中,我执行以下操作:
var key = event.keyCode;
if (key == 13 && !event.shiftKey)
I am using this function on a textarea. 我在文本区域上使用此功能。
When I press enter everything works except one thing: A line break is made within the textarea which should be prevented. 当我按Enter键时,除以下事项外,其他所有功能均起作用:在文本区域内出现换行符,应避免该行。 How to avoid? 如何避免?
This is how you can do it: 这是您可以执行的操作:
function(event) {
event.preventDefault();
var key = event.keyCode;
if (key == 13 && !event.shiftKey) ...
}
event.preventDefault() also works for links and in any other case you want to prevent the default action of an event. event.preventDefault()也适用于链接,并且在任何其他情况下,您都想阻止事件的默认操作。
Another important function sometimes is event.stopPropagation()
. 有时另一个重要功能是event.stopPropagation()
。
More information here: https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault 此处的更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault
and here: https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation 此处: https : //developer.mozilla.org/zh-CN/docs/Web/API/Event/stopPropagation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.