jQuery Mobile C...login
jQuery Mobile Classic Tutorial
author:php.cn  update time:2022-04-11 13:58:34

jQuery Mobile events



Event = response action for all different visitors to the page.


jQuery Mobile Events

In jQuery Mobile you can use any standard jQuery event.

In addition, jQuery Mobile also provides events for mobile browsers:

  • Touch event - triggered when the user touches the screen

  • Sliding events - triggered when the user slides up or down

  • Orientation events - triggered when the device is flipped horizontally or vertically

  • Page events - triggered when the page is shown, hidden, created, loaded or not loaded


Initialize jQuery Mobile events

We learned when learning jQuery Use $(document).ready() to cause your jQuery code script to start executing after the DOM element is loaded:

Example

<!DOCTYPE html>
<html>
<head>
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").on("click",function(){
    $(this).hide();
  });
});
</script>
</head>
<body>

<p>如果您点击我,我会消失。</p>
<p>点击我,我会消失。</p>
<p>点击我,我也会消失。</p>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

However, in jQuery Mobile, use the pageinit event to set the code script to start after the DOM element is loaded. Execution, so to load and create a script that executes on any new page, you need to bind the pageinit event.

The second parameter ("#pageone") is the page id of the specified event:

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).on("pagecreate","#pageone",function(){
  $("p").on("click",function(){
    $(this).hide();
  });                       
});
</script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>页眉文本</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>如果您点击我,我会消失。</p>
    <p>点击我,我会消失。</p>
    <p>点击我,我也会消失。</p>
  </div>

  <div data-role="footer">
    <h1>页脚文本</h1>
  </div>
</div> 

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).on("pagecreate","#pageone",function(){
  $("p").on("click",function(){
    $(this).hide();
  });                       
});
</script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>页眉文本</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>如果您点击我,我会消失。</p>
    <p>点击我,我会消失。</p>
    <p>点击我,我也会消失。</p>
  </div>

  <div data-role="footer">
    <h1>页脚文本</h1>
  </div>
</div> 

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).on("pagecreate","#pageone",function(){
  $("p").on("click",function(){
    $(this).hide();
  });                       
});
</script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>页眉文本</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>如果您点击我,我会消失。</p>
    <p>点击我,我会消失。</p>
    <p>点击我,我也会消失。</p>
  </div>

  <div data-role="footer">
    <h1>页脚文本</h1>
  </div>
</div> 

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance



# Note: The jQuery on() method is used to bind events to the selected element.
We will introduce jQuery Mobile events in more detail in the next chapter.