當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。