欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

添加 增加 js 文件

shiping1 的头像
      因为Drupal内置了jQuery,所以在Drupal       会自动加载 jQuery。在 Drupal 中,可通过
中使用jQuery很简单,在添加JavaScript时      drupal_add_js()函数来添加JavaScript文件。
 
一  页面直接添加 jQuery
1,以管理员的身份登录网站;
2,开启PHP filter模块;
3,创建一个页面,选择PHP code 的输入格式;
4,输入
<?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>
5,刷新后即可看到效果。
 
二  在主题中使用 jQuery
包含JavaScript文件的最方便的方式,即   码放在你主题下的一个名为 logofade.js 的文在主题的.info文件中添加一行代码,不过这种    件中。例如,如使用copu主题,那么它就位于方式也有一个缺点,那就是缺乏灵活性。     themes/copu/logofade.js。
为站点添加一个效果,用来强调你站点的标识语,在一个页面被加载时,先把它淡出,
接着再把它渐显出来。把下面的JavaScript代
63
// Selects the theme element with the id "logo", fades it out, // then fades it in slowly.
jQuery(document).ready(function(){ jQuery("#logo").fadeOut("fast").fadeIn("slow");
});
JavaScript文件已经有了;再加载它就可以了。在当前主题的.info文件中添加下面一行代码:三  在模块内使用 jQueryscripts[] = logofade.js
最后一步让Drupal重读.info文件,这样它就会看到它需要加载logofade.js了。
我们最常使用jQuery设计模块添加功能。下面举例介绍模块中jQuery的添加。本模块它识和隐藏,并提供了一个有用的按钮用来将区块重新显示出来。
使用jQuery来对左右边栏区域中的区块进行标
 
信息文件:blockaway.info name = Block-Away description = Uses jQuery to hide blocks until a button is clicked.
package = Pro Drupal Development core = 7.x files[]=blockaway.module
模块文件blockaway.module,通过函数载入js文件。下面的代码使用hook_theme()声明了一个主题函数,并将 drupal_add_js()调用移到了该主题函数中,而在 hook_init()中调用这个主题函数。
<?php
/**
*  @file
*  Use this module to learn about jQuery.
*/
/**
*  Implements hook_init().
*/
function blockaway_init() { theme('blockaway_javascript');
64
}
/**
*  Implements hook_theme().
*  Register our theme function.
*/
function blockaway_theme() { return array(
'blockaway_javascript' => array(
'arguments' => array(),
),
);
}
/**
*  Theme function that just makes sure our JavaScript file * gets included.
*/
function theme_blockaway_javascript() {
drupal_add_js(drupal_get_path('module', 'blockaway') .'/blockaway.js'); }
 
我们将使用主题提供的JavaScript,来覆写模块提供的 JavaScript。设置blockaway.js到你的当前主题下 ----例如,themes/copu/blockaway.js。设置特效效果:以5秒钟的时间来渐进的显示区块。下面是文件:
/**
*  Hide blocks in sidebars, then make them visible at the click of a button.
*/
jQuery(document).ready(function() {
// Get all div elements of class 'block' inside the left sidebar.
// Add to that all div elements of class 'block' inside the // right sidebar. var blocks = jQuery('#sidebar-first div.block, #sidebar-second div.block'); // Hide them. blocks.hide();
65
// Add a button that, when clicked, will make them reappear. jQuery('#sidebar-first').prepend('<div id="collapsibutton">Show Blocks</div>'); jQuery('#collapsibutton').css({
'width': '90px',
'border': 'solid',
'border-width': '1px',
'padding': '5px',
'background-color': '#fff'
});
// Add a handler that runs once when the button is clicked.
jQuery('#collapsibutton').one('click', function() { // Button clicked! Get rid of the button.
jQuery('#collapsibutton').remove();
// Display all our hidden blocks using an effect. blocks.fadeIn("5000");
});
});
最后,需要让Drupal加载这个新文件,从而取代sites/all/modules/custom/blockaway中的
原文件。我们 通过覆写主题函数来实现这一点。在主题的template.php文件中添加函数:
/**
* Override theme_blockaway_javascript() with the * following function.
*/
function bartik_blockaway_javascript() { drupal_add_js(path_to_theme() . '/blockaway.js');
}
现在,当使用浏览器来访问一个页面时,就能够看到“显示区块”按钮了,点击这个按钮,将
会使用一个渐进的淡入效果来显示区块。

来自 http://yp.oss.org.cn/blog/show_resource.php?resource_id=2017
普通分类: