您的位置:

如何使用oninput改善网站用户体验

随着移动设备和移动网络的不断发展,用户对于网站的响应速度和交互体验提出了越来越高的要求。其中,输入框作为用户和网站进行交互的枢纽,其使用的体验尤为重要。而oninput事件,可以帮助开发者在用户输入时及时做出响应,从而改善网站的用户体验。

一、自动完成输入

oninput事件可以让网站在用户输入时,实时地检测输入框的内容,并给出相应的建议或结果。如,在搜索框中输入关键词时,网站可以自动为用户推荐相关的搜索关键词,或在下拉框中呈现搜索结果。这样,用户可以少打几个字,直接得到自己想要的答案,大大提升了使用效率。

代码示例:

<input id="search" type="text" oninput="autoComplete()">
<ul id="auto-complete"></ul>
<script>
function autoComplete() {
  // 获取用户输入
  var input = document.getElementById('search').value;
  // 向后端请求相似结果
  var results = getResults(input);
  // 展示结果
  var list = document.getElementById('auto-complete');
  list.innerHTML = '';
  for(var i = 0; i < results.length; i++) {
    var li = document.createElement('li');
    li.innerText = results[i];
    list.appendChild(li);
  }
}
</script>

以上代码演示了一个简单的自动完成输入的功能。在输入文本时,oninput事件调用了autoComplete函数,该函数向后端请求相似结果,并将结果展示在页面上。

二、输入时验证表单

oninput事件还可以让网站在用户输入时检测表单中的输入内容,以帮助用户及时发现并纠正输入错误。如,在输入框中输入格式错误的手机号码或电子邮件地址时,网站可以在用户输入过程中即时提示用户,并在表单提交之前进行验证,防止用户输入错误内容。

代码示例:

<input type="text" id="phone" oninput="validatePhone()">
<span id="phone-error-message" style="color: red; display: none;">请输入正确的手机号码</span>
<script>
function validatePhone() {
  // 获取用户输入
  var phone = document.getElementById('phone').value;
  // 验证手机号码格式
  if(!/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/.test(phone)) {
    document.getElementById('phone-error-message').style.display = 'block';
  } else {
    document.getElementById('phone-error-message').style.display = 'none';
  }
}
</script>

以上代码演示了一个简单的手机号码输入验证功能。在输入文本时,oninput事件调用了validatePhone函数,该函数检测用户输入的手机号码格式是否正确,并在页面上展示相应的提示信息。

三、实时预览输入效果

oninput事件还可以让网站在用户输入时实时预览输入的效果。如,在文本编辑器中输入富文本内容时,网站可以在用户输入过程中即时地展示输入的效果,让用户更直观地感受到自己的输入效果。

代码示例:

<div id="editor" contenteditable="true" oninput="preview()"></div>
<div id="preview"></div>
<script>
function preview() {
  // 获取编辑器内容
  var content = document.getElementById('editor').innerHTML;
  // 预览内容
  document.getElementById('preview').innerHTML = content;
}
</script>

以上代码演示了一个简单的富文本编辑预览功能。在输入文本时,oninput事件调用了preview函数,该函数获取编辑器中的内容,并在页面上展示预览效果。

结语:

通过以上介绍,我们可以看出,oninput事件的应用非常广泛。在实际的网站开发中,我们可以根据具体的需求,灵活运用oninput事件,提升网站的用户体验,让用户感受到更加顺畅和自然的交互体验。

如何使用oninput改善网站用户体验

2023-05-17
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)