1. 概述 jquery允许拓展自定义的方法, 绑定到$.fn对象上, 编写一个jQuery插件的原则:给$.fn绑定函数,实现插件的代码逻辑;插件函数最后要return this;以支持链式调用; 插件函数要有默认值,绑定在$.fn.<pluginNam ...
1. 概述
jquery允许拓展自定义的方法, 绑定到$.fn对象上,
编写一个jQuery插件的原则:
- 给
$.fn
绑定函数,实现插件的代码逻辑; - 插件函数最后要
return this;
以支持链式调用; - 插件函数要有默认值,绑定在
$.fn.<pluginName>.defaults
上; - 用户在调用时可传入设定值以便覆盖默认值。
2. example
<html><body><div id="test-highlight1"> <p>什么是<span>jQuery</span></p> <p><span>jQuery</span>是目前最流行的<span>JavaScript</span>库。</p></div><script src='/images/loading.gif' data-original="jquery-3.1.0.js"></script><script type="text/javascript">$(function(){ 'use strict'; (function(){ $.fn.highlight = function (options) { var opts = $.extend({}, $.fn.highlight.defaults, options); // this已绑定为当前jQuery对象: this.css('backgroundColor', opts.backgroundColor).css('color', opts.color); return this; } $.fn.highlight.defaults = { 'backgroundColor' : '#d85030', 'color' : '#fff8de' }; $.fn.highlight.defaults.backgroundColor = '#659f13'; $.fn.highlight.defaults.color = '#f2fae3'; $("#test-highlight1 span").highlight(); $('#test-highlight1 span').highlight({
原标题:jquery 拓展
关键词:jquery
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。