The document provides an introduction and overview of jQuery Mobile (jQM) including:
- Downloading and including the jQuery Mobile framework
- The basic page structure of jQM including headers, footers, and content sections
- Common jQuery Mobile components like headers, footers, lists, and forms
2. Contents
Introduction to jQuery Mobile (jQM)
Getting started with jQM
-Downloading the Most Recent Version of jQuery Mobile
-Proper Markup for Loading Framework JavaScript and CSS
jQuery Mobile Page Structure
-Page Anatomy: Header, Footer and Content Sections
-Header and Footer Toolbars
-Bundling Pages into a Single Document
-Navigating Between Pages
Applying Different Theme Swatches
Page Initialization Events
jQuery Mobile Page Components
Basic Content Formatting
List Views
-Ordered and Unordered Lists
-Inset Lists
-Lists with Links
-Nested Lists
-Lists with Icons or Thumbnail Images
-Split Button Lists
-List Dividers
-Search Filters
Form Controls - check boxes, slider, etc.
3. Introduction to jQuery Mobile (jQM)
• jQuery is a JavaScript Library.
• jQuery greatly simplifies JavaScript
programming.
• jQuery is easy to learn
4. Introduction to jQuery Mobile (jQM)
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
5. jQuery Introduction
• The purpose of jQuery is to make it much
easier to use JavaScript on your website.
6. What is jQuery
• jQuery is a lightweight, "write less, do more",
JavaScript library.
• The purpose of jQuery is to make it much
easier to use JavaScript on your website.
7. What is jQuery
• jQuery takes a lot of common tasks that
require many lines of JavaScript code to
accomplish, and wraps them into methods
that you can call with a single line of code
8. What is jQuery
• The jQuery library contains the following
features:
– HTML/DOM manipulation
– CSS manipulation
– HTML event methods
– Effects and animations
– AJAX
– Utilities
9. Why jQuery
• The jQuery library contains the following
features:
– Easy to use
– Less LOC
– Simplify code
– Rich api
10. jQuery Install
• There are several ways to start using jQuery
on your web site. You can:
– Download the jQuery library from jQuery.com
– Include jQuery from a CDN, like Google
11. Downloading jQuery
• There are two versions of jQuery available for
downloading:
– Production version - this is for your live website
because it has been minified and compressed
– Development version - this is for testing and
development (uncompressed and readable code)
http://jquery.com/download/
12. Downloading jQuery
• The jQuery library is a single JavaScript file,
and you reference it with the HTML <script>
tag (notice that the <script> tag should be
inside the <head> section):
<head>
<script src="jquery-1.11.1.min.js"></script>
</head>
13. jQuery CDN
• If you don't want to download and host
jQuery yourself, you can include it from a CDN
(Content Delivery Network).
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jque
ry.min.js"></script>
</head>
14. jQuery CDN
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
15. jQuery Syntax
• With jQuery you select (query) HTML
elements and perform "actions" on them.
• The jQuery syntax is tailor made
for selecting HTML elements and performing
some action on the element(s).
16. jQuery Syntax
• Basic syntax is: $(selector).action()
– A $ sign to define/access jQuery
– A (selector) to "query (or find)" HTML elements
– A jQuery action() to be performed on the
element(s)
17. jQuery Syntax
• Examples:
– $(this).hide() - hides the current element.
– $("p").hide() - hides all <p> elements.
– $(".test").hide() - hides all elements with
class="test".
– $("#test").hide() - hides the element with
id="test".
18. The Document Ready Event
$(document).ready(function()
{
// jQuery methods go here...
});
This is to prevent any jQuery code from running
before the document is finished loading (is
ready).
19. The Document Ready Event
• It is good practice to wait for the document to
be fully loaded and ready before working with
it.
• This also allows you to have your JavaScript
code before the body of your document, in
the head section.
20. The Document Ready Event
• Here are some examples of actions that can
fail if methods are run before the document is
fully loaded:
– Trying to hide an element that is not created yet
– Trying to get the size of an image that is not
loaded yet
21. The Document Ready Event
• The jQuery team has also created an even
shorter method for the document ready
event:
$(function(){
// jQuery methods go here...
});
23. The element Selector
• All selectors in jQuery start with the dollar
sign and parentheses: $().
• The jQuery element selector selects elements
based on the element name.
• You can select all <p> elements on a page like
this:
24. jQuery selectors
• jQuery selectors are one of the most
important parts of the jQuery library
• jQuery selectors allow you to select and
manipulate HTML element(s)
• jQuery selectors are used to "find" (or select)
HTML elements based on their id, classes,
types, attributes, values of attributes
25. jQuery selectors
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
26. The #id Selector
• The jQuery #id selector uses the id attribute of
an HTML tag to find the specific element.
• An id should be unique within a page, so you
should use the #id selector when you want to
find a single, unique element.
27. The #id Selector
• To find an element with a specific id, write a
hash character, followed by the id of the HTML
element:
$("#test")
28. The #id Selector
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
29. The .class Selector
• The jQuery class selector finds elements with
a specific class.
• To find elements with a specific class, write a
period character, followed by the name of the
class:
$(".test")
•
30. The .class Selector
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
</script>
</head>
<body>
<h2 class="test">This is a heading</h2>
<p class="test">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
31. The .class Selector
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("*").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
32. The .class Selector
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
33. The .class Selector
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p.intro").hide();
});
});
</script>
</head>
<body>
<h2 class="intro">This is a heading</h2>
<p class="intro">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
36. Functions In a Separate File
• If your website contains a lot of pages, and
you want your jQuery functions to be easy to
maintain, you can put your jQuery functions in
a separate .js file.
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min
.js">
</script>
<script src="my_jquery_functions.js"></script>
</head>
39. What are Events?
• All the different visitor's actions that a web
page can respond to are called events.
• An event represents the precise moment
when something happens.
• Examples:
– moving a mouse over an element
– selecting a radio button
– clicking on an element
40. What are Events?
Mouse Events Keyboard
Events
Form Events Document/Window
Events
click keypress submit load
dblclick keydown change resize
mouseenter keyup focus scroll
mouseleave blur unload
41. jQuery Syntax For Event Methods
• In jQuery, most DOM events have an
equivalent jQuery method.
• To assign a click event to all paragraphs on a
page, you can do this:
42. jQuery Syntax For Event Methods
$("p").click();
• The next step is to define what should happen
when the event fires. You must pass a function
to the event:
$("p").click(function(){
// action goes here!!
});
47. Header and Footer Toolbars
• Toolbar elements are often placed inside
headers and footers - for "easy-access"
navigation:
48. Header Bars
• The header is located at the top of the page
and usually contain a page title/logo or one or
two buttons (typically home, options or
search).
• You can add buttons to the left and/or to the
right side in the header.
49. Header Bars
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left">Home</a>
<h1>Welcome To My Homepage</h1>
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-search ui-btn-icon-left">Search</a>
</div>
<div data-role="main" class="ui-content">
<p>Notice that we have added the CSS class of "ui-corner-all" and "ui-shadow" to the header buttons, to make them look a bit more
desirable.</p>
</div>
</div>
</body>
</html>
52. Footer Bars
• The footer is located at the bottom of the
page.
• The footer is more flexible than the header - it
is more functional and changeable throughout
pages, and can therefore contain as many
buttons as needed:
• The buttons in the footer are not centered by
default. To fix this, simply use CSS:
<div data-role="footer" style="text-align:center;">
53. Footer Bars<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left ">Home</a>
<h1>Welcome To My Homepage</h1>
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-search ui-btn-icon-left ">Search</a>
</div>
<div data-role="main" class="ui-content">
<p>The buttons are for demonstration purposes only, and will not have any effect.</p>
<p>Notice that we have applied the CSS class of "ui-corner-all" and "ui-shadow" to the buttons in both the header and footer, to make them look more desirable.</p>
</div>
<div data-role="footer">
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">Add Me On Facebook</a>
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">Add Me On Twitter</a>
<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">Add Me On Instagram</a>
</div>
</div>
</body>
</html>
54. Positioning Headers and Footers
• The header and the footer can be positioned
in three ways:
– Inline - Default. Headers and footers are inline
with the page content
– Fixed - Headers and footers will remain positioned
at the top and bottom of the page
– Fullscreen - Behaves like fixed; headers and
footers will remain positioned at the top and
bottom, but also over the page content. It is also
slightly see-through
55. Positioning Headers and Footers
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed">
<h1>Inline Header</h1>
</div>
<div data-role="main" class="ui-content">
<p><b>Tip:</b> To see the effect, try to resize the window if the scrollbar is not available.</p>
<p>Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to
enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..</p>
</div>
<div data-role="footer" data-position="inline">
<h1>Inline Footer</h1>
</div>
</div>
</body>
</html>
56. Navigating Between Pages
• A navigation bar consists of a group of links
that are aligned horizontally, typically within a
header or footer
• The bar is coded as an unordered list of links
wrapped inside a <div> element that has the
data-role="navbar" attribute
59. Active Buttons
• When a link in the navbar is tapped/clicked, it
gets the selected (pressed down) look.
• To achieve this look without having to tap the
link, use the class="ui-btn-active":
60. <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page" id="pageone">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
<div data-role="navbar">
<ul>
<li><a href="#" class="ui-btn-active" data-icon="home">Home</a></li>
<li><a href="#pagetwo" data-icon="arrow-r">Page Two</a></li>
</ul>
</div>
</div>
<div data-role="main" class="ui-content">
<p>With the ui-btn-active class, notice that the Home button stays highlighted (selected).</p>
<p>If you click on the Page Two, you will notice that none of the buttons are highlighted (selected).</p>
</div>
<div data-role="footer">
<h1>My Footer</h1>
</div>
</div>
<div data-role="page" id="pagetwo">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
<div data-role="navbar">
<ul>
<li><a href="#pageone" data-icon="home">Home</a></li>
<li><a href="#" data-icon="arrow-r">Page Two</a></li>
</ul>
</div>
</div>
<div data-role="main" class="ui-content">
<p>No buttons are pre-selected (highlighted) in this page..</p>
<p>To get the selected look for each button that represents the page you are actually on, go back to our navbar tutorial and read the next step to find out how!</p>
</div>
<div data-role="footer">
<h1>My Footer</h1>
</div>
</div>
</body>
</html>
62. Applying Different Theme Swatches
• jQuery Mobile provides two different style
themes, "a" and "b" - each with different
colors for buttons, bars, content blocks, and so
on.
63. Applying Different Theme Swatches
• To customize the look of your application, use
the data-theme attribute, and assign the
attribute with a letter:
<div data-role="page" data-theme="a|b">
67. jQuery Mobile Pages
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
</div>
<div data-role="main" class="ui-content">
<p>I Am Now A Mobile Developer!!</p>
</div>
<div data-role="footer">
<h1>Footer Text</h1>
</div>
</div>
</body>
</html>
68. jQuery Mobile Pages
• The data-role="page" is the page displayed in the
browser
• The data-role="header" creates a toolbar at the top of
the page (often used for title or search buttons)
• The data-role="main" defines the content of the page,
like text, images, buttons, forms, etc.
• The "ui-content" class adds extra padding and margin
inside the page content
• The data-role="footer" creates a toolbar at the bottom
of the page
• Inside these containers, you can add any HTML
elements - paragraphs, images, headings, lists, etc.
69. Adding Pages in jQuery Mobile
• In jQuery Mobile, you can create multiple
pages in a single HTML file.
• Separate each page with a unique id and use
the href attribute to link between them:
70. Adding Pages in jQuery Mobile
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page" id="pageone">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
</div>
<div data-role="main" class="ui-content">
<p>Welcome! If you click on the link below, it will take you to Page Two.</p>
<a href="#pagetwo">Go to Page Two</a>
</div>
<div data-role="footer">
<h1>Footer Text</h1>
</div>
</div>
<div data-role="page" id="pagetwo">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
</div>
<div data-role="main" class="ui-content">
<p>This is Page Two. If you click on the link below, it will take you to Page One.</p>
<a href="#pageone">Go to Page One</a>
</div>
<div data-role="footer">
<h1>Footer Text</h1>
</div>
</div>
</body>
</html>
71. Using Pages as Dialogs
• A dialog box is a type of window used to show
special information or request input.
• To create a dialog box that opens when a user
taps on a link, add data-dialog="true" to the
page you want displayed as a dialog:
72. Using Pages as Dialogs
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page" id="pageone">
<div data-role="header">
<h1>Welcome To My Homepage</h1>
</div>
<div data-role="main" class="ui-content">
<p>Welcome!</p>
<a href="#pagetwo">Go to Dialog Page</a>
</div>
<div data-role="footer">
<h1>Footer Text</h1>
</div>
</div>
<div data-role="page" data-dialog="true" id="pagetwo">
<div data-role="header">
<h1>I'm A Dialog Box!</h1>
</div>
<div data-role="main" class="ui-content">
<p>The dialog box is different from a normal page, it is displayed on top of the current page and it will not span the entire width of the page. The dialog has also an icon of "X" in the header to
close the box.</p>
<a href="#pageone">Go to Page One</a>
</div>
<div data-role="footer">
<h1>Footer Text In Dialog</h1>
</div>
</div>
</body>
</html>
74. jQuery Mobile List Views
• List views in jQuery Mobile are standard HTML
lists; ordered (<ol>) and unordered (<ul>).
• To create a list, apply the data-role="listview"
to the <ol> or <ul> element. To make the
items tappable, specify a link inside each list
item (<li>):
78. List Dividers
• List dividers are used to organize and group
items into categories/sections.
• To specify a list divider, add the data-role="list-
divider" attribute to an <li> element:
80. List Dividers
• If you have an alphabetically list, (for example
a phone book) jQuery Mobile automatically
adds appropriate dividers by setting the data-
autodividers="true" attribute on the <ol> or
<ul> element:
83. jQuery Mobile List Icons
• The default icon for each list item containing a
link is "carat-r" (right arrow).
• To change this to another icon, use the data-
icon attribute on the list item you want to
modify:
85. jQuery Mobile List Icons
data-icon="false" will remove the icon
• To add a standard 16x16px icon to your list,
add an <img> element inside the link with a
class of "ui-li-icon":