jQuery開發外掛模板
阿新 • • 發佈:2019-01-01
使用jQuery開發一個小外掛的模板寫法:
HTML程式碼:
<span id="test">test</span>
JS程式碼:
<script type="text/javascript">
!function ($) {
"use strict";
var Plugins =function(element,option){
this.$element =element;
this.defaults = {
"color":"green",
"fontSize" :"16px",
"fontWeight":"bold"
}
this.options = $.extend({}, this.defaults, option)
}
Plugins.prototype = {
changecss:function(){
return this.$element.css({
'color':this.options.color,
'fontSize':this .options.fontSize,
"fontWeight":this.options.fontWeight
})
}
}
$.fn.changeCss = function(options){
var myPlugins = new Plugins(this,options);
return myPlugins.changecss();
}
}(window.jQuery);
</script>
呼叫方式:
$("#aaa").changeCss ({
"color" :"yellow",
"fontSize":"20px",
"fontWeight":"bold"
})