jquery

介绍

特点&技巧

  • Every method within jQuery returns the query object itself

$("a").addClass("test").show().html("foo");
  • Stop normal event fire.

e.preventDefault();
  • the $(“a.remote”, this) query, this is passed as a context: For the document ready event, this refers to the document, and it therefore searches the entire document for anchors with class remote.
  • handler(foobar); handler.apply(this, [foobar]); // if you need the context of the original handler
  • Most plugins can be used like this: Include the plugin file and call the plugin method on some elements, passing some optional settings to customize the plugin.

常用代码

UI组件

Plugin开发

  • 取名,比如check,对应plugin js文件名:jquery.check.js
  • 定义方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
jQuery.fn.check = function(mode) {
   // if mode is undefined, use 'on' as default
   var mode = mode || 'on';
   
   return this.each(function() {
     switch(mode) {
       case 'on':
         this.checked = true;
         break;
       case 'off':
         this.checked = false;
         break;
       case 'toggle':
         this.checked = !this.checked;
         break;
     }
   });
 };
  • 使用
1
2
3
4
$("input[@type='checkbox']").check();
 $("input[@type='checkbox']").check('on');
 $("input[@type='checkbox']").check('off');
 $("input[@type='checkbox']").check('toggle');

Rails集成

对比prototype

资源


Wiki首页 | 查看所有 | 编辑 | 输出到博客 | 历史版本