jQuery in Action

68
CHAPTER 3
Bringing pages to life with jQuery
return name.split(" ");
}
else {
return [];
}
};
But don't worry about the specifics of the syntax for extending jQuery; we'll go
into that in more detail in chapter 7. What's important is that we can use
get-
ClassNames()
anywhere in our script to obtain an array of class names or an
empty array if an element has no classes. Nifty!
Now that we've learned how to get and set the styles of elements, let's discuss
different ways for modifying their contents.
3.3 Setting element content
When it comes to modifying the contents of elements, there's an ongoing debate
regarding which technique is better: using
DOM
API methods or changing their
inner
HTML
. In most cases, modifying an element's
HTML
is easier and more
effective, so jQuery gives us a number of methods to do so.
3.3.1 Replacing HTML or text content
First is the simple
html()
command, which allows us to retrieve the
HTML
con-
tents of an element when used without parameters or, as we've seen with other
jQuery functions, to set its contents when used with a parameter.
Here's how to get the
HTML
content of an element:
Here's how to set the
HTML
content of all matched elements:
Command syntax: html
html()
Obtains the HTML content of the first element in the matched set.
Parameters
none
Returns
The HTML content of the first matched element. The returned value is identical to accessing
the innerHTML property of that element.


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.