jQuery
Selectors
Selector
|
Example
|
Selects
|
$("*")
|
All elements
|
|
$("#lastname")
|
The element with
id="lastname"
|
|
$(".intro")
|
All elements with
class="intro"
|
|
$(".intro.demo")
|
All elements with the classes
"intro" and "demo"
|
|
$("p")
|
All p elements
|
|
$("h1,div,p")
|
All h1, div and p elements
|
|
|
|
|
$("p:first")
|
The first p element
|
|
$("p:last")
|
The last p element
|
|
$("tr:even")
|
All even tr elements
|
|
$("tr:odd")
|
All odd tr elements
|
|
|
|
|
$("div p:first-child")
|
The first p element of all div
elements
|
|
$("div p:last-child")
|
The last p element of all div
elements
|
|
$("div p:nth-child(2)")
|
The p element that is the second
child of all div elements
|
|
$("div p:only-child")
|
The p element that is the only
child of all div elements
|
|
|
|
|
$("div > p")
|
All p elements that are a direct
child of a div element
|
|
$("div p")
|
All p elements that are
descendants of a div element
|
|
$("div + p")
|
The p element that are next to
each div elements
|
|
$("div ~ p")
|
All p elements that are siblings
of a div element
|
|
|
|
|
$("ul li:eq(3)")
|
The fourth element in a list
(index starts at 0)
|
|
$("ul li:gt(3)")
|
List elements with an index
greater than 3
|
|
$("ul li:lt(3)")
|
List elements with an index less
than 3
|
|
$("input:not(:empty)")
|
All input elements that are not
empty
|
|
|
|
|
$(":header")
|
All header elements h1, h2 ...
|
|
$(":animated")
|
All animated elements
|
|
$(":focus")
|
The element that currently has
focus
|
|
$(":contains('Hello')")
|
All elements which contains the
text "Hello"
|
|
$("div:has(p)")
|
All div elements that have a p
element
|
|
$(":empty")
|
All elements that are empty
|
|
$(":parent")
|
All elements that are a parent of another
element
|
|
$("p:hidden")
|
All hidden p elements
|
|
$("table:visible")
|
All visible tables
|
|
|
|
|
$("[href]")
|
All elements with a href attribute
|
|
$("[href='default.htm']")
|
All elements with a href attribute
value equal to "default.htm"
|
|
$("[href!='default.htm']")
|
All elements with a href attribute
value not equal to "default.htm"
|
|
$("[href$='.jpg']")
|
All elements with a href attribute
value ending with ".jpg"
|
|
$("[hreflang|='en']")
|
All elements with a hreflang
attribute value starting with "en"
|
|
$("[name^='hello']")
|
All elements with a name attribute
value starting with "hello"
|
|
$("[name~='hello']")
|
All elements with a name attribute
value containing the word "hello"
|
|
$("[name*='hello']")
|
All elements with a name attribute
value containing the string "hello"
|
|
|
|
|
$(":input")
|
All input elements
|
|
$(":text")
|
All input elements with
type="text"
|
|
$(":password")
|
All input elements with
type="password"
|
|
$(":radio")
|
All input elements with
type="radio"
|
|
$(":checkbox")
|
All input elements with
type="checkbox"
|
|
$(":submit")
|
All input elements with
type="submit"
|
|
$(":reset")
|
All input elements with
type="reset"
|
|
$(":button")
|
All input elements with
type="button"
|
|
$(":image")
|
All input elements with
type="image"
|
|
$(":file")
|
All input elements with
type="file"
|
|
$(":enabled")
|
All enabled input elements
|
|
$(":disabled")
|
All disabled input elements
|
|
$(":selected")
|
All selected input elements
|
|
$(":checked")
|
All checked input elements
|
Mouse Events
|
Keyboard Events
|
Form Events
|
Document/Window Events
|
click
|
keypress
|
submit
|
load
|
dblclick
|
keydown
|
change
|
resize
|
mouseenter
|
keyup
|
focus
|
scroll
|
mouseleave
|
|
blur
|
unload
|
$("p").click(function(){
// action goes here!!
});
// action goes here!!
});
jQuery
Event Methods
Event methods trigger or attach a
function to an event handler for the selected elements.
The following table lists all the
jQuery methods used to handle events.
Method
|
Description
|
Attaches event handlers to
elements
|
|
Attaches/Triggers the blur event
|
|
Attaches/Triggers the change event
|
|
Attaches/Triggers the click event
|
|
Attaches/Triggers the double click
event
|
|
Attaches a handler to current, or
future, specified child elements of the matching elements
|
|
Removes all event handlers added
with the live() method
|
|
Attaches/Triggers the error event
|
|
The current DOM element within the
event bubbling phase
|
|
Contains the optional data passed
to an event method when the current executing handler is bound
|
|
Returns the element where the
currently-called jQuery event handler was attached
|
|
Returns whether
event.preventDefault() was called for the event object
|
|
Returns whether
event.stopImmediatePropagation() was called for the event object
|
|
Returns whether
event.stopPropagation() was called for the event object
|
|
Returns the namespace specified
when the event was triggered
|
|
Returns the mouse position
relative to the left edge of the document
|
|
Returns the mouse position
relative to the top edge of the document
|
|
Prevents the default action of the
event
|
|
Returns which element being entered or exited
on mouse movement.
|
|
Contains the last/previous value
returned by an event handler triggered by the specified event
|
|
Prevents other event handlers from
being called
|
|
Prevents the event from bubbling
up the DOM tree, preventing any parent handlers from being notified of the
event
|
|
Returns which DOM element
triggered the event
|
|
Returns the number of milliseconds
since January 1, 1970, when the event is triggered
|
|
Returns which event type was
triggered
|
|
Returns which keyboard key or
mouse button was pressed for the event
|
|
Attaches/Triggers the focus event
|
|
Attaches an event handler to the
focusin event
|
|
Attaches an event handler to the
focusout event
|
|
Attaches two event handlers to the
hover event
|
|
Attaches/Triggers the keydown
event
|
|
Attaches/Triggers the keypress
event
|
|
Attaches/Triggers the keyup event
|
|
Adds one or more event handlers to
current, or future, selected elements
|
|
Attaches an event handler to the
load event
|
|
Attaches/Triggers the mousedown
event
|
|
Attaches/Triggers the mouseenter
event
|
|
Attaches/Triggers the mouseleave
event
|
|
Attaches/Triggers the mousemove
event
|
|
Attaches/Triggers the mouseout
event
|
|
Attaches/Triggers the mouseover
event
|
|
Attaches/Triggers the mouseup
event
|
|
Removes event handlers attached
with the on() method
|
|
Attaches event handlers to
elements
|
|
Adds one or more event handlers to
selected elements. This handler can only be triggered once per element
|
|
Takes an existing function and
returns a new one with a particular context
|
|
Specifies a function to execute
when the DOM is fully loaded
|
|
Attaches/Triggers the resize event
|
|
Attaches/Triggers the scroll event
|
|
Attaches/Triggers the select event
|
|
Attaches/Triggers the submit event
|
|
Attaches two or more functions to
toggle between for the click event
|
|
Triggers all events bound to the
selected elements
|
|
Triggers all functions bound to a
specified event for the selected elements
|
|
Removes an added event handler
from selected elements
|
|
Removes an event handler to
selected elements, now or in the future
|
|
Attaches an event handler to the
unload event
|
Example
with Callback
$("button").click(function(){
$("p").hide("slow",function(){
alert("The paragraph is now hidden");
});
});
$("p").hide("slow",function(){
alert("The paragraph is now hidden");
});
});
Get
Content - text(), html(), and val()
Three simple, but useful, jQuery
methods for DOM manipulation is:
- text() - Sets or returns the text content of selected elements
- html() - Sets or returns the content of selected elements (including HTML markup)
- val() - Sets or returns the value of form fields
The following example demonstrates
how to get content with the jQuery text() and html() methods:
Example
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
Get
Attributes - attr()
The jQuery attr() method is used to
get attribute values.
The following example demonstrates
how to get the value of the href attribute in a link:
Example
$("button").click(function(){
alert($("#w3s").attr("href"));
});
alert($("#w3s").attr("href"));
});
Set
Attributes - attr()
The jQuery attr() method is also
used to set/change attribute values.
The following example demonstrates
how to change (set) the value of the href attribute in a link:
Example
$("button").click(function(){
$("#w3s").attr("href","http://www.w3schools.com/jquery");
});
$("#w3s").attr("href","http://www.w3schools.com/jquery");
});
The attr() method also allows you to
set multiple attributes at the same time.
The following example demonstrates
how to set both the href and title attributes at the same time:
Example
$("button").click(function(){
$("#w3s").attr({
"href" : "http://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
});
$("#w3s").attr({
"href" : "http://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
});
Query
width() and height() Methods
The width() method sets or returns
the width of an element (includes NO padding, border, or margin).
The height() method sets or returns
the height of an element (includes NO padding, border, or margin).
The following example returns the
width and height of a specified <div> element:
Example
$("button").click(function(){
var txt="";
txt+="Width: " + $("#div1").width() + "</br>";
txt+="Height: " + $("#div1").height();
$("#div1").html(txt);
});
var txt="";
txt+="Width: " + $("#div1").width() + "</br>";
txt+="Height: " + $("#div1").height();
$("#div1").html(txt);
});
jQuery
innerWidth() and innerHeight() Methods
The innerWidth() method returns the
width of an element (includes padding).
The innerHeight() method returns the
height of an element (includes padding).
The following example returns the
inner-width/height of a specified <div> element:
Example
$("button").click(function(){
var txt="";
txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
txt+="Inner height: " + $("#div1").innerHeight();
$("#div1").html(txt);
});
var txt="";
txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
txt+="Inner height: " + $("#div1").innerHeight();
$("#div1").html(txt);
});
jQuery
outerWidth() and outerHeight() Methods
The outerWidth() method returns the
width of an element (includes padding and border).
The outerHeight() method returns the
height of an element (includes padding and border).
The following example returns the
outer-width/height of a specified <div> element:
Example
$("button").click(function(){
var txt="";
txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
txt+="Outer height: " + $("#div1").outerHeight();
$("#div1").html(txt);
});
var txt="";
txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
txt+="Outer height: " + $("#div1").outerHeight();
$("#div1").html(txt);
});
The outerWidth(true) method returns
the width of an element (includes padding, border, and margin).
The outerHeight(true) method returns
the height of an element (includes padding, border, and margin).
Example
$("button").click(function(){
var txt="";
txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
$("#div1").html(txt);
});
var txt="";
txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
$("#div1").html(txt);
});
jQuery
More width() and height()
The following example returns the
width and height of the document (the HTML document) and window (the browser
viewport):
Example
$("button").click(function(){
var txt="";
txt+="Document width/height: " + $(document).width();
txt+="x" + $(document).height() + "\n";
txt+="Window width/height: " + $(window).width();
txt+="x" + $(window).height();
alert(txt);
});
var txt="";
txt+="Document width/height: " + $(document).width();
txt+="x" + $(document).height() + "\n";
txt+="Window width/height: " + $(window).width();
txt+="x" + $(window).height();
alert(txt);
});
The following example sets the width
and height of a specified <div> element:
Example
$("button").click(function(){
$("#div1").width(500).height(500);
});
$("#div1").width(500).height(500);
});
jQuery
HTML / CSS Methods
The following table lists all the
methods used to manipulate the HTML and CSS.
The methods below work for both HTML
and XML documents. Exception: the html() method.
Method
|
Description
|
Adds one or more class names to
selected elements
|
|
Inserts content after selected
elements
|
|
Inserts content at the end of
selected elements
|
|
Inserts HTML elements at the end
of selected elements
|
|
Sets or returns attributes/values
of selected elements
|
|
Inserts content before selected
elements
|
|
Makes a copy of selected elements
|
|
Sets or returns one or more style
properties for selected elements
|
|
Removes selected elements (keeps
data and events)
|
|
Removes all child nodes and
content from selected elements
|
|
Checks if any of the selected
elements have a specified class name
|
|
Sets or returns the height of
selected elements
|
|
Sets or returns the content of
selected elements
|
|
Returns the height of an element
(includes padding, but not border)
|
|
Returns the width of an element
(includes padding, but not border)
|
|
Inserts HTML elements after
selected elements
|
|
Inserts HTML elements before
selected elements
|
|
Sets or returns the offset
coordinates for selected elements (relative to the document)
|
|
Returns the first positioned
parent element
|
|
Returns the height of an element
(includes padding and border)
|
|
Returns the width of an element (includes
padding and border)
|
|
Returns the position (relative to
the parent element) of an element
|
|
Inserts content at the beginning
of selected elements
|
|
Inserts HTML elements at the
beginning of selected elements
|
|
Sets or returns properties/values
of selected elements
|
|
Removes the selected elements
(including data and events)
|
|
Removes one or more attributes
from selected elements
|
|
Removes one or more classes from
selected elements
|
|
Removes a property set by the
prop() method
|
|
Replaces selected elements with
new HTML elements
|
|
Replaces selected elements with
new content
|
|
Sets or returns the horizontal
scrollbar position of selected elements
|
|
Sets or returns the vertical
scrollbar position of selected elements
|
|
Sets or returns the text content
of selected elements
|
|
Toggles between adding/removing
one or more classes from selected elements
|
|
Removes the parent element of the
selected elements
|
|
Sets or returns the value
attribute of the selected elements (for form elements)
|
|
Sets or returns the width of
selected elements
|
|
Wraps HTML element(s) around each
selected element
|
|
Wraps HTML element(s) around all
selected elements
|
|
Wraps HTML element(s) around the
content of each selected element
|
No comments:
Post a Comment