禁用页面的右键菜单
1  | $(document).ready(function(){  | 
新窗口打开页面
1  | 
  | 
输入框文字获取和失去焦点【推荐】
1  | 
  | 
返回头部滑动动画
1  | <script>  | 
获取鼠标位置
1  | 
  | 
关闭所有 jQuery 动画效果
1  | jQuery.fx.off = true;  | 
检测鼠标的右键和左键
1  | $('#box').mousedown(function(e) {  | 
回车提交表单
1  | $(function() {  | 
切换复选框
1  | var tog = false;  | 
使用 siblings() 来选择同辈元素
1  | //不这样做  | 
为任何与选择器相匹配的元素绑定事件
1  | $("table").on("click","td",function(){  | 
$.proxy 的使用
使用回调方法的缺点之一是当执行类库中的方法后,上下文对象被设置到另外一个元素,比如,执行下面代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #panel {
      width: 300px;
      height: 300px;
      background-color: #ccc;
    }
  </style>
  <script src="jquery.js"></script>
</head>
<body>
  <div id="panel" style="display:none">
    <button>Close</button>
  </div>
  <script>
  $('#panel').fadeIn(function(){
    $('#panel button').click(function(){
      $(this).fadeOut();
    });
  });
  </script>
</body>
</html>
你将遇到问题,button 元素会消失,而不是 panel 元素。可以使用 $.proxy 方法解决这个问题,代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #panel {
      width: 300px;
      height: 300px;
      background-color: #ccc;
    }
  </style>
  <script src="jquery.js"></script>
</head>
<body>
  <div id="panel" style="display:none">
    <button>Close</button>
  </div>
  <script>
  $('#panel').fadeIn(function(){
    $('#panel button').click($.proxy(function(){
      $(this).fadeOut();
    },this));
  });
  </script>
</body>
</html>
这样才正确执行。
限制 Text-Area 域中的字符的个数
1  | 
  | 
解析 json 数据时报 parseError 错误
jQuery 在 1.4 版本后,采用了更为严格的 json 解析方式,即所有内容都必须要有双引号,如果升级 jQuery 版本后,ajax 加载 json 报错,有可能就是这个原因。比如:1
2
3
4
5// 1.4之前版本,key没引号,这样没问题
{
  key:"coco",
  status:"0"
}
但升级成jQuery1.4后,都必须加上双引号,格式如下:1
2
3
4{
  "key":"coco",
  "status":"0"
}
从元素中除去 HTML
1  | <script>  | 
扩展 String 对象的方法
1  | 
  |