注意单引号的转义,由于我们在drupal_add_js()内部已经使用了一对单引号,所以需要对双引号内部的单引号进行转义。
' f% P$ u& ^) L( q修改CSS元素的值(Changing Values of CSS Elements)
0 t& v% \1 d5 B( SjQuery can be used to assign (or reassign) values to CSS elements. Let’s set the border surrounding the first paragraph to solid (see Figure 18-4):
# x5 g4 o0 x7 ajQuery("#one").wrap("<div class=\'error\'></div>").css("border", "solid");
- Y5 S; b3 n# R1 ]Notice that the css method is still acting on the p element, not on the div element, because the wrap method returned the targeted p element after wrapping it.
, V% ]7 J! ]: J二、在主题中使用jQuery
Adding JavaScript via a Theme .info File
( U/ \' K* R$ y. i# bThe most convenient but least flexible way to include JavaScript files is to include a line in your theme’s .info file. Let’s add an effect to your site that emphasizes the logo of your site by making it fade out and then fade in again when a page is loaded. Place the following JavaScript code in a file called logofade.js in your current theme. For example, if you are using the Bartik theme, it would be at themes/bartik/logofade.js. C' S- q, H/ Y- e! T, |
包含JavaScript文件的最方便的方式,就是在你的主题的.info文件中添加一行代码,不过这种方式也有一个缺点,那就是缺乏灵活性。让我们为你的站点添加一个效果,用来强调你站点的标识语,在一个页面被加载时,先把它淡出,接着再把它渐显出来。把下面的JavaScript代码放在你主题下的一个名为logofade.js的文件中。例如,如果你使用的主题为Bartik,那么它就位于themes/Bartik/logofade.js。
5 p* S2 v- P0 g* q0 Y. Y( c+ B// Selects the theme element with the id "logo", fades it out,% j% |) t) D+ {( h, h! ?# B, x
// then fades it in slowly.
2 Q- E+ o2 e7 xjQuery(document).ready(function(){
" G( |- g* ? a* Z; v1 JjQuery("#logo").fadeOut("fast").fadeIn("slow");6 y) ~* I/ t) e/ E4 y
});
The JavaScript file is in place; now we just have to tell Drupal to load it. Add the following line to your current theme’s .info file:
JavaScript文件已经有了;现在我们只需要告诉Drupal加载它就可以了。向你的当前主题的.info文件中添加下面一行代码:
scripts[] = logofade.js
The last step is to make Drupal reread the .info file so that it will see that it needs to load logofade.js. To do that, go to Appearance, temporarily switch to a different theme, and then switch back.
最后一步就是让Drupal重读.info文件,这样它就会看到它需要加载logofade.js了。为了实现这一点,导航到“管理➤站点构建➤主题”,临时的转换到一个不同的主题上,然后再转换回来。
This method of adding JavaScript is useful if the JavaScript will be loaded on every single page of your web site. In the next section, you’ll see how to add JavaScript only when a module that uses it is enabled.
如果对于你网站的每个页面,都需要为其加载某一JavaScript文件的话,那么这个方法还是非常有用的。在接下来的一节中,你将看到如何实现,只有当使用JavaScript的模块被启用时,才加载它。
2 e" P4 g" j2 m) T2 Q三、在模块内使用jQuery
The following module does just that,using jQuery to identify and hide the blocks in the left and right sidebar regions and providing a helpful button that will bring the blocks back. Here’s sites/all/modules/custom/blockaway/blockaway.info:
/ m, a2 h/ a6 V' W' j G) t; a下面的模块实现了这一点,它使用jQuery来对左右边栏区域中的区块进行标识和隐藏,并提供了一个有用的按钮用来将区块重新显示出来。
name = Block-Away7 J6 J6 T$ a2 T. P
description = Uses jQuery to hide blocks until a button is clicked.
- s! |, ]$ \) x; W$ F5 G! jpackage = Pro Drupal Development
! o& V" A+ X0 {: o9 W+ E5 Ucore = 7.x
" V9 R% H1 i+ V$ Q. T" l; W6 |6 xfiles[]=blockaway.module
And here’s sites/all/modules/custom/blockaway/blockaway.module:
<?php6 L' [$ d# H+ p( A
/**
4 Q. b2 G9 Q( M* @file
; U) T6 c6 M# ~) d2 H* f8 c7 B* Use this module to learn about jQuery.
- f! D* O, n$ r# N, k' m*/3 L! \6 ?9 j$ H' `& Z# q) f$ I, Z
/**2 }) o; _9 m* F- t: k F. W8 X& W) N
* Implements hook_init().
2 |& u0 `- l4 V# ?1 C*/
) ]& K" R+ s9 x3 p; n2 p0 _function blockaway_init() {# g: n) H+ x& A1 E. ~
drupal_add_js(drupal_get_path('module', 'blockaway') .'/blockaway.js');
$ Q; x2 t$ v8 t* h9 }}
All the module does is include the following JavaScript file, which you can put at6 g7 X+ L# g( Q$ D" K
sites/all/modules/custom/blockaway/blockaway.js:
/**9 D. `1 i" {% R6 c. H
* Hide blocks in sidebars, then make them visible at the click of a button.
1 \ E+ Q- k4 _ \ U*/
2 t& z, l0 _) l9 L; F/ D ^* ejQuery(document).ready(function() {
2 U) v0 y8 v1 Y7 |: r( f// Get all div elements of class 'block' inside the left sidebar." A2 o7 X0 B0 ?6 p( |
// Add to that all div elements of class 'block' inside the# s' {" F$ A7 P1 W( a6 [0 i# d5 M
// right sidebar. Check your theme’s page.tpl.php file to see what
9 t5 v% p+ i% T$ X3 ]/ m, p+ J// selectors you should use – the following are for garland./ l7 y1 \4 x" F7 ?
var blocks = jQuery('#sidebar-first div.block, #sidebar-second div.block');/ x& j: y; m0 G F1 t
// Hide them.- @! \* q. U G6 q
blocks.hide();
' O( I6 f, n$ F5 X' }1 P7 S// Add a button that, when clicked, will make them reappear.
+ S, B. G v$ C% w: i. \jQuery('#sidebar-first').prepend('<div id="collapsibutton">Show Blocks</div>');
6 R* F7 u% h# Q& `5 K$ UjQuery('#collapsibutton').css({) y! f" g( v) N7 V' d& l. R
'width': '90px',
& ^9 a& j4 \; [1 m8 {'border': 'solid',+ ~: H' \: q$ o9 e; v1 Y0 V1 b
'border-width': '1px',$ `0 ^- K1 Z3 ^3 |. g
'padding': '5px'," Q2 H( I6 _9 S( f' y. _! b
'background-color': '#fff': J. y" P7 ?* [" Y
});3 k, G1 m2 \6 A/ w/ L
// Add a handler that runs once when the button is clicked.
% `- I; d7 Y8 H" D. W4 z4 U$ Q% IjQuery('#collapsibutton').one('click', function() {6 R' c! j& o" a! f
// Button clicked! Get rid of the button.
+ b/ x+ e8 M7 c7 J d2 S- hjQuery('#collapsibutton').remove();
: @$ [# |! P( B6 U Z// Display all our hidden blocks using an effect.4 |2 p* M, q* s- c* `3 L8 O
blocks.slideDown("slow");
5 Z5 r& u' b7 l0 V, m8 O! L/ O});
. W- W0 @ l! q0 C% {: _0 g});
When you enable the module on the Modules page, any blocks you have visible should disappear and be replaced with a plain button.
9 a. V$ f1 _6 z- f' I* @% o导航到“管理➤站点构建➤模块”,启用该模块,你以前可见的所有区块都消失了,被替换为了一个没有格式的按钮。
6 n) h; Y" K, }% [ N三、可覆写的JavaScript (Overridable JavaScript)2 S+ I8 r. |9 D! ]7 x2 R: ]4 B
The code in blockaway.module is simple and easy to understand. It just makes sure the blockaway.js file is included.However, if the module were more complicated, it would be friendlier to others to put the drupal_add_js() function call in a theme function instead of in hook_init(). That way, those who wanted to use your module but customize the JavaScript code in some way could do so without touching your module code at all (see Chapter 9 for how the theme system works its magic). The code that follows is a revised version of blockaway.module that declares a theme function using hook_theme(), moves the drupal_add_js() call into the theme function, and calls the theme function from hook_init(). The functionality is the same, but savvy developers can now override the blockaway.js file.
9 o" I7 g0 X" ?% jblockaway.module中的代码非常简单并易于理解。它只负责包含blockaway.js文件。然而,如果模块更加复杂一点的话,那么将 drupal_add_js()函数调用放在一个主题函数中,来取代放在hook_init()中,这对其他开发者来说会更加友好。这样,对于那些想使用你的模块,同时又想用一些方式来定制JavaScript代码的用户,他们无需修改你模块的源代码就可以完成定制工作(关于主题系统的更多详细,可参看第 8章)。下面的代码是blockaway.module的修订版本,它使用hook_theme()声明了一个主题函数,并将 drupal_add_js()调用移到了该主题函数中,而在hook_init()中调用这个主题函数。功能是一样的,但是现在聪明的开发者就可以对 blockaway.js文件进行覆写了。
9 O5 G9 Z" p% l3 z0 @
<?php5 u% J8 {" c( S0 C- O
/**- p. W# G9 [1 H/ l) g& V
* @file- `+ Q4 s o! l+ C# E
* Use this module to learn about jQuery.
& t5 X" r7 t1 n9 a; b*/
4 B' ]* f X" ^6 Y1 g/**
( g5 A0 ~/ i" w* Implements hook_init().
) F% v4 J: B" f- n*/
s7 c) u5 C1 U, hfunction blockaway_init() {
- ^8 D/ [+ ]/ L1 h5 Mtheme('blockaway_javascript');
8 g6 ^& L2 B' w* }3 A% `}0 C" {8 D) m; [. A" Z9 F
/**0 h& d W1 s1 ?6 j8 y2 q
* Implements hook_theme().9 ` c3 N: t$ t2 Z5 v, W2 l
* Register our theme function.& w5 V" f/ e2 v, a9 f
*/
- x! Y4 d9 I K3 p8 }function blockaway_theme() {
6 N( c- f' R3 L5 B6 X% Ireturn array(
3 r4 F3 s+ |8 j'blockaway_javascript' => array(6 e2 r& p3 d% Q1 L
'arguments' => array(),
( B: L5 H: R, Y2 O( O) R& j),
( a; R) S; C6 C* J1 `6 }8 P, Y);, W. R h/ B; H$ x; B; L( T! v
}
1 H* n6 w" G( `6 H8 `9 c/**( t8 H/ N& o# b
* Theme function that just makes sure our JavaScript file
" \& W' W" D* k4 Y; B* gets included.( D5 _! i: t$ b' g
*/" |3 c% T3 H: Y9 o2 ^+ J3 J
function theme_blockaway_javascript() {$ P; E, P" @8 k: Q2 s
drupal_add_js(drupal_get_path('module', 'blockaway') .'/blockaway.js');9 o1 [" V* ^2 C" V9 N( i/ r) g9 n; i* x
}
) Y. _& G% \) \8 H
覆写的JavaScript文件:
; G" w' B9 P! \- ELet’s go ahead and see if this approach works. We’re going to override the JavaScript provided by the module with JavaScript provided by the theme. Copy sites/all/modules/custom/blockaway/lockaway.js to your current theme—for example, themes/bartik/ blockaway.js. Let’s change the JavaScript file slightly so that we’ll know which JavaScript file is being used. Change the effect from slideDown("slow") to fadeIn(5000); this will fade in the blocks over a period of five seconds. Here is the new file:
# L, [5 J: F7 B6 I# N( f让我们继续前进,来看看这种方法是否可以工作。我们将使用主题提供的JavaScript,来覆写模块提供的 JavaScript。把sites/all/modules/custom/blockaway/blockaway.js拷贝到你的当前主题下 ----例如,themes/bartik/blockaway.js。让我们稍微的修改一下JavaScript文件,这样我们就知道正在使用的是哪个JavaScript文件。将特效从slideDown("slow")改为fadeIn(5000);这将使用5秒钟的时间来渐进的显示区块。下面是新文件:
4 @$ l+ M3 N' Y# R5 w
/**
3 i+ W& a/ o6 c7 ~( h& m* Hide blocks in sidebars, then make them visible at the click of a button.7 k3 {# j4 a, j- f! d
*/. @, Y6 N L$ L9 Y3 Y
jQuery(document).ready(function() {, ~8 T% V+ s( t
// Get all div elements of class 'block' inside the left sidebar.
: r( m, H o0 E0 _# Z1 i# ~// Add to that all div elements of class 'block' inside the
$ w: G3 K4 G7 l9 S// right sidebar.
" f- E0 z+ h) X; c* Y# h. @: Ivar blocks = jQuery('#sidebar-first div.block, #sidebar-second div.block');
+ S7 d: \; x; i- c+ O// Hide them.+ m5 Y. {! i5 t0 R
blocks.hide();
# [ _' l! X+ l7 R3 s) w+ Z// Add a button that, when clicked, will make them reappear.
* ?, v' F, O4 u* ]1 djQuery('#sidebar-first').prepend('<div id="collapsibutton">Show Blocks</div>');
( W3 e, R' }! W8 X: G$ x6 \jQuery('#collapsibutton').css({. E* C! K2 A1 b2 w8 X
'width': '90px',
I% q7 m( v3 s2 p P'border': 'solid',
2 N# X2 f4 o* Y'border-width': '1px',
1 [% F7 K @5 t: }+ X'padding': '5px',. ?. _7 J0 i! P7 q& t6 E/ h
'background-color': '#fff'
3 Z) ^) `5 s% Q6 |1 N7 T( h});& j9 C- T: c. _0 d* R5 m% u' i+ l
// Add a handler that runs once when the button is clicked.- y5 z( `% [3 F7 @) V
jQuery('#collapsibutton').one('click', function() {
/ p. N& m! V' T6 g7 R( G// Button clicked! Get rid of the button.7 ~, n) I* v$ |( }0 b3 J) V
jQuery('#collapsibutton').remove();# i" p7 J. d. v( A' z$ d
// Display all our hidden blocks using an effect.
/ F7 L+ Q, @: {3 wblocks.fadeIn("5000");
! ~+ [9 W3 R# A8 l2 q2 u% t});, e5 ?& L* ~- X8 V) {1 i" ` A) D2 [
});
+ S/ a& V( o( ?The last change we need to make is to tell Drupal to load this new JavaScript file instead of the one in sites/all/modules/custom/blockaway. We do that by overriding the theme function. Add the following function to the template.php file of your theme (if your theme doesn’t have a template.php file,it’s okay to create one):) O$ o r2 t0 _
我们最后要做的修改就是告诉Drupal加载这个新文件,从而取代sites/all/modules/custom/blockaway中的文件。我们通过覆写主题函数来实现这一点。向你主题的template.php文件中(如果你的主题还没有一个template.php文件,那么你需要创建一个),添加下面所给的函数:
. c1 {5 n/ X4 { C
/**8 w$ q& o$ C( }
* Override theme_blockaway_javascript() with the
2 j I$ i1 P3 U- C8 M* following function.
6 _) r: M2 ?! Q! M) ]/ |- ]*/
# S# l$ d" q) [# M# \! Xfunction bartik_blockaway_javascript() {
( Z4 j8 n2 o% Z' j1 cdrupal_add_js(path_to_theme() . '/blockaway.js');
% s* R8 C7 F @, F8 [$ T* b5 p! ?' N% h}
Note:Change the name of the preprocess function so that it uses the name of the theme you are using. In the preceding example, I am using the Bartik theme.
Visit the Modules page to rebuild the theme registry so your changes will be recognized. When you visit a page in your web browser, you should see the Show Blocks button, and clicking it should reveal the blocks via a gradual fade-in effect instead of the slide effect we were using earlier. Congratulations!
现在,当你使用浏览器来访问一个页面时,你应该能够看到“显示区块”按钮了,点击这个按钮,将会使用一个渐进的淡入效果来显示区块,而不是我们前面所用的幻灯效果。恭喜恭喜!8 m/ D7 h* n- N/ z2 x& |0 n
You’ve learned how to use jQuery in your module, how to write it in a way that is friendly to themers and other developers, and coincidentally, how to cleanly override or enhance JavaScript files provided by other module developers who have been equally courteous.
你已经学会了如何在你的模块中使用jQuery,如何使用友好的方式编写jQuery以方便主题制作者和其他的开发者,同时,你还学会了如何覆写或增强其它模块中的JavaScript文件,这里假定这些模块中的JavaScript文件可被覆写。
1 V, G8 l& p% eBefore we leave this example, let me demonstrate how to override a template file. First, remove the bartik_blockaway_javascript() function that you added to the template.php file. Next, in your current theme, create an empty file called blockawayjavascript. tpl.php. For example, if you are using the Bartik theme, create themes/bartik/blockaway-javascript.tpl.php. Don’t put anything inside this file.) H) y( Y' D8 z! q- Q' L
Now visit the Modules page. The act of visiting this page will rebuild the theme registry. Drupal will find the template file and use it instead of the theme function in your module. The result is that blockaway.js will never be loaded; you’ve essentially commented out the theme function by creating an empty template file (recall from Chapter 9 that, when building the theme registry, Drupal will look for a template file and then for theme functions).
在我们结束这个例子的学习以前,让我演示一下如何使用模板文件进行覆写。首先,删除你添加到template.php文件中的 bartik_blockaway_javascript()函数。接着,在你的当前主题中,创建一个空文件 blockawayjavascript.tpl.php。例如,如果你使用的是Bartik主题,那么创建的就是themes/Bartik/blockaway-javascript.tpl.php。不要在这个文件中放置任何东西。现在导航到模块页面,访问这个页面的作用就是重新构建主题注册表。Drupal 将找到该模板文件,并使用它来替代你模块中的主题函数。最终的结果就是永远也不会加载blockaway.js了;通过创建一个空的模板文件,你实质上就是注释掉了该主题函数。
Now, add the following to your blockaway-javascript.tpl.php file:
现在,向你的blockaway-javascript.tpl.php文件中添加以下代码:
<?php drupal_add_js(path_to_theme() . '/blockaway.js'); ?>
When you reload your page, you should see that the JavaScript file is now loading. Do you see how these techniques can be useful for substituting your own enhanced JavaScript file in a third-party module or for preventing some JavaScript from loading?
当你重新加载页面时,你应该可以看到JavaScript文件现在加载进来了。如果你想将第3方模块中的JavaScript文件替换为你的增强版本,或者想阻止加载一些JavaScript文件时,这里所讲的这些技术还是很有用处的。
You cannot call drupal_add_js() from inside page.tpl.php or any theme functions that are called in its preprocessing (such as blocks), because they are executed too late in the page building process. See modules/block/block-admin-display-form.tpl.php for an example of a core template file that adds JavaScript.
你不能在page.tpl.php中调用drupal_add_js(),对于其它的一些主题函数,如果是在它的预处理阶段调用的话(比如区块),那么也不能使用drupal_add_js()。为什么呢?这是因为它们在页面构建流程中执行的顺序过于靠后。核心模板文件是如何添加 JavaScript的,可参看modules/block/block-admin-display-form.tpl.php。
) v% a' Q3 D3 l% y3 S) j! ?; O
本文摘自 Drupal爱好者,谢谢!
" r! j1 s6 {6 Q( H. O