How to use Drupal Behaviors

1. in your node add below scripts.

    <a href="#" id="togg-link">Click this link to toggle the content displayed below</a>
    <div id="togg">
    This content will be toggled open and closed using jQuery.
    Blah, blah, blah.
    </div>

 2. create a new file under   /sites/all/themes/xxx/patrick.js

Drupal 6:

Drupal.behaviors.togg = function (context) {
	$('a#togg-link:not(.togg-processed)', context).addClass('togg-processed').each(function () {
	$(this).click(function() {
		$("div#togg").toggle(400);
			return false;
		});
	});
};

 Drupal 7:

(function ($) {
	Drupal.behaviors.togg = {
		attach: function (context, settings) {
			$('a#togg-link:not(.togg-processed)', context).addClass('togg-processed').each(function() {
				$(this).click(function() {
					$("div#togg").toggle(400);
					return false;
				});
			});
		}
	};
})(jQuery);

3, please clear drupal cache

4. try to refresh the node and see the effect

你可能感兴趣的:(drupal)