Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
63
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

CHAPTER 18 USING JQUERY

Using a CSS Class Selector

Here’s a similar example using a CSS class selector instead of using a CSS ID as we did in the preceding section. The HTML would be as follows:

<p class="intro">Welcome to the World of Widgets.</p>

<p class="intro">Widgets are available in many sizes.</p>

Our CSS would look like this:

.intro { background-color: blue;

}

The following would also work, and is a slightly more specific rule:

p.intro { background-color: blue;

}

Here’s how the CSS translates to jQuery code:

jQuery(".intro").css("background-color", "blue").fadeIn("slow");

or

jQuery("p.intro").css("background-color", "blue").fadeIn("slow");

In the first of the preceding examples, you’re asking jQuery to find any HTML element that has the intro class, while in the second example you ask for any paragraph tag with an intro class. Note that the last example will be slightly faster because there’s less HTML for jQuery to search through, given the example’s restriction to just the paragraph tags using p.intro.

Tip In CSS, the dot is a class selector that can be reused within a document, and the hash refers to a unique ID selector whose name can occur only once per page.

Now that you’ve had a taste of how jQuery works, let’s see it in action within Drupal.

jQuery Within Drupal

Using jQuery within Drupal is easy because jQuery is preinstalled and is automatically made available when JavaScript is added. In Drupal, JavaScript files are added via the drupal_add_js() function or in the theme’s .info file. In this section, you’ll investigate some basic jQuery functionality within Drupal.

392

CHAPTER 18 USING JQUERY

Your First jQuery Code

Let’s get set up to play with jQuery.

1.Log into your Drupal site as user 1 (the administrative account).

2.On the Modules page, enable the PHP filter module.

3.Create a new node of type Basic Page, but on the node creation form, be sure to select “PHP code” under the “Input formats” section, as shown in Figure 18- 2. Enter Testing jQuery as the title, and add the following to the body section of the form:

<?php

drupal_add_js('jQuery(document).ready(function () { jQuery("p").hide(); jQuery("p").fadeIn("slow");

});', 'inline');

?>

<p id="one">Paragraph one</p> <p>Paragraph two</p> <p>Paragraph three</p>

4.Click Submit, and reload the page. The three paragraphs you created will slowly fade in. Cool, eh? Refresh the page to see it again. Let’s study this example a little more.

393

CHAPTER 18 USING JQUERY

Figure 18-2. Experimenting with jQuery using the PHP filter

The jQuery code is contained in a file, misc/jquery.js. This file is not loaded for every page within Drupal. Instead, anytime a drupal_add_js() call is made, jquery.js is loaded. Two parameters are passed into drupal_add_js(). The first parameter is the JavaScript code you wish to have executed, and the second parameter (inline) tells Drupal to write the code inside a pair of <script></script> tags within the document’s <head> element.

Note We’re using drupal_add_js() quite simply here, but it has many more possibilities, which you can discover at http://api.drupal.org/api/function/drupal_add_js/7.

Let’s look at the JavaScript jQuery code in more detail.

394

CHAPTER 18 USING JQUERY

<?php

drupal_add_js('jQuery(document).ready(function () { jQuery("p").hide(); jQuery("p").fadeIn("slow");

});', 'inline');

?>

The jQuery(document).ready function needs a little more explanation. When the browser is rendering a page, it gets to a point where it has received the HTML and fully parsed the DOM structure of the page. The next step is to render that DOM, which includes loading additional local—and possibly even remote—files. If you try to execute JavaScript code before the DOM has been generated, the code may throw errors and not run because the objects it wants to manipulate are not there yet. JavaScript programmers used to get around this by using some variation of the following code snippet:

window.onload = function(){ ... }

The difficulty with using window.onload is that it has to wait for the additional files to also load, which is too long of a wait. Additionally, the window.onload approach allows the assignment of only a single function. To circumvent both problems, jQuery has a simple statement that you can use:

jQuery(document).ready(function(){ // Your code here.

});

jQuery(document).ready() is executed just after the DOM is generated. You’ll always want to wrap jQuery code in the preceding statement for the reasons listed earlier. The function() call defines an anonymous function in JavaScript—in this case, containing the code you want to execute.

That leaves us with the actual meat of the code, which ought to be self-explanatory at this point:

//Hide all the paragraphs. jQuery("p").hide();

//Fade them into visibility. jQuery("p").fadeIn("slow");

The preceding code finds all paragraph tags, hides them, and then slowly reveals them within the page. In jQuery lingo, the fadeIn() part is referred to as a method. The “p” isn’t preceded by a “.” or “#” due to the p being a HTML tag instead of a CSS class (“.”) or ID (“#”).

395

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]