当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


JQuery jQuery.fn.extend()用法及代码示例

用法
jQuery.fn.extend( object ) => Object

说明:将对象的内容合并到 jQuery 原型上,以提供新的 jQuery 实例方法。

  • 添加的版本:1.0jQuery.fn.extend( object )

    • object
      类型:Object
      要合并到 jQuery 原型上的对象。
jQuery.fn.extend() 方法扩展了 jQuery 原型 ($.fn) 对象以提供可以链接到 jQuery() 函数的新方法。

例子:

向 jQuery 原型 ($.fn) 对象添加两个方法,然后使用其中一个。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.fn.extend demo</title>
  <style>
  label {
    display: block;
    margin: .5em;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<label><input type="checkbox" name="foo"> Foo</label>
<label><input type="checkbox" name="bar"> Bar</label>
 
<script>
jQuery.fn.extend({
  check: function() {
    return this.each(function() {
      this.checked = true;
    });
  },
  uncheck: function() {
    return this.each(function() {
      this.checked = false;
    });
  }
});
 
// Use the newly created .check() method
$( "input[type='checkbox']" ).check();
</script>
 
</body>
</html>

演示:

相关用法


注:本文由纯净天空筛选整理自jquery.com大神的英文原创作品 jQuery.fn.extend()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。