In mobile web development, sometimes we need to close the soft keyboard after the input box gains focus to better display the page content. In the iOS system, jQuery can be used to achieve this function. Below we will introduce the corresponding code implementation method in detail.
1. Introduction
jQuery is a very powerful JavaScript library that provides us with convenient DOM operations and event binding methods. By using jQuery, we can easily manipulate DOM elements, modify styles, bind events and other operations. In iOS, we can use jQuery to operate the input box and realize the function of automatically closing the soft keyboard.
2. Code implementation
The code implementation of jQuery is very simple and can be completed with only a few lines of code. The specific implementation code is as follows:
$(document).ready(function() { $('input, textarea').on('focus', function(e) { e.preventDefault(); $('[data-toggle="keyboard"]').blur(); }); });
Here we bind the focus
events of the input
and textarea
elements to gain focus in the input box When, we cancel the processing of the default event through the e.preventDefault()
method, and then use $('[data-toggle="keyboard"]').blur()
to change the page The hidden <input>
element gains focus and loses focus, so that the soft keyboard is automatically closed.
3. Notes
During the implementation process, there are several points that need attention:
<script src="//cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
meta tag of
viewport needs to be set. The code is as follows:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<input> element on the page to get focus when the soft keyboard is closed. The code looks like this:
<input type="text" class="keyboard" data-toggle="keyboard" style="position: absolute; top: 0; left: 0; width: 1px; height: 1px; opacity: 0;">
data-toggle="keyboard" attribute here is used to find the element in the jQuery code.
The above is the detailed content of How to close ios keyboard in jquery. For more information, please follow other related articles on the PHP Chinese website!