我们在input框中输入内容的时候,可能会有输入空格的情况,但是在我们的开发过程中,一般都不能有空格的,所以我们要在用户输入的过程中,将空格过滤掉。接下来掩饰的这种方法在微信小程序中是不适用的。代码如下:
一、我们要在input标签中使用onkeypress事件
1、onkeypress事件:在用户按下键盘按钮时执行Javascript代码
2、在input标签中写入代码onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<style>
input {
width: 300px;
height: 30px;
margin: 50px 50px;
border: 1px solid black;
border-radius: 4px;
}
</style>
</head>
<body>
<div>
<input type="text" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;" />
</div>
</body>
</html>
注:空格的keycode为32