<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>boilerplate模板插件测试</title>
<script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
<script language="javascript">
// JavaScript Document
;(function ( $, window, document, undefined ) {
var pluginName = 'test',
defaults = {
propertyName: "value"
};
function Plugin ( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this._version = "version 0.1.0";
this.init();
}
/* 对外私有de方法--private */
var privateFunc = function () {
$(this).html("我的内容在初始化时,被私有函数修改了");
};
/* 构造函数方法-private */
var create = function () {
};
/* 对外暴露de方法 */
Plugin.prototype.setContent = function (val) {
$(this.element).html(val);
return $(this.element).html();
};
/* 获取和设置参数变量--get|set 方法 */
Plugin.prototype.get = function(v){
try{
if( typeof v == 'string' )
return eval("this.options."+v);
else
throw new Error("Invilid parameter type!");
}catch(e){
alert(e.message);
}
};
Plugin.prototype.set = function(v,l){
try{
if( typeof v == 'string' )
eval("this.options." + v + "= l;");
else
throw new Error("Invilid parameter type!");
}catch(e){
alert(e.message);
}
};
/* 初始化方法 */
Plugin.prototype.init = function () {
create.call(this);//构造函数
privateFunc.call(this);//私有函数调用方法
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
}
})(jQuery, window, document);
$(document).ready(function(){
var $obj = $("#plugin").test({propertyName:12}).data("plugin_test");
alert("提示:初始化已结束!");
var str = $obj.setContent("我的内容在初始化后,被外部调用的公有函数修改了");
alert(str);
$obj.set("propertyName",232);
alert("propertyName:"+$obj.get("propertyName"));
});
</script>
</head>
<body>
<div id="plugin">test内容</div>
<span></span>
</body>
</html>
版权声明:本文为博主原创文章,未经博主允许不得转载。