Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Introduction
JQuery is JavaScript code thats been written for you. JQuery is an add-on library
for JavaScript and it does the hard JavaScript coding work for you.
JQuery lets you easily change the appearance, location, or behavior of an
element on a Web page. You can use jQuery effects on elements on your page,
but jQuery also lets you control when these effects take place.
This code stores the value of the src attribute from an <img>
element into the imageSource variable
The above two line says, Get the HTML content of the <p> element and save it
in the pContent variable. The second line says, Display in an alert box the value
stored in the pContent variable.
Setting element attribute values Suppose that you want to change an
image displayed on a page. Because the displayed image is based on what is
stored in the <img> src attribute, changing the src value will change which
image is displayed on the page. See the following codes:
The first line gets the HTML content inside the <strong> element and stores it
in the strongContent variable. The second line stores the content of the <p>
element in the pContent variable.
$(strong).html(pContent);
$(p).html(strongContent);
The first line puts the HTML code stored in pContent in the <strong> element.
The second puts the HTML code stored in strongContent in the <p> element.
Changing Text Content Sometimes you dont want the actual HTML code
in an element; you want only the text. To do so, replace the html() function with
the text() function. In the preceding example, you swapped the HTML code. If
you want to swap only the text and not the HTML code, use this code:
<html>
<head>
<title>My Test Page</title>
<script
type=text/javascript
src=js/jquery1.4.min.js></script>
<script type=text/javascript>
$(document).ready(function(){
var strongContent = $(strong).text();
var pContent = $(p).text();
$(strong).text(pContent);
$(p).text(strongContent);
});
</script>
</head>
<body>
<strong>This is the text in the <em>STRONG</em>
element.</strong>
Note that in the above example the <em> tags are no longer present, so
nothing is italicized. Using the text() method strips out HTML elements.
$(*).attr({id: myNewID});
You shouldnt use the * character with some jQuery functions.
Consider this line of code:
$(*)text(Not a good idea.);
When this line of code executes, you end up with this HTML code on
your page: <html>Not a good idea.</html>
alert ($(#moreTxt).text());
This code opens an alert box containing the text stored in the
selector named with the id attribute moreTxt.
here are three class attributes assigned to a single <p> element: <p
attribute=hfrstclass anotherclass dogclass catclassh>
c) A class attribute is used in jQuery with a period (.) in front of the class
name. In HTML code, for example, I use class = myclass. But when I use
the class with a selector in the jQuery code, I place a period in front of the
class name, that is, .myclass.
Selecting by order When you have several of the same type of elements on a
page, you may want to select by order. For example, you might want to select
the third <p> element on the page. jQuery has a set of functions that lets you
$(img:first).attr(src);
b) :last Selects the last matching element. This code returns the value of the
src attribute of the last <img>, which is images/cover2.jpg.
$(img:last).attr(src);
c) :even Matches even elements, starting with 0. This code changes the text
of the first and third <strong> elements (see Figure 3-5).
f) :gt(index) Selects all elements with an index value greater than the index.
Selected elements are elements below the selected element on the page.
g) :lt(index) Selects all elements with an index value less than the index.
Selected elements are above the selected element on the page.