jQuery.extend({
min : function(a,b){
return a<b?a:b;
},
max : function(a,b){
return a<b?b:a;
}
})
$(function(){
$("input").click(function(){
var a = prompt("请输入一个数:");
var b = prompt("再输入一个数:");
var c = jQuery.min(a,b);
var d = jQuery.max(a,b);
alert("最大值是:" + d + "\n最小值是:" + c);
});
})
<input type="button" value="jQuery扩展测试" />
jQuery.extend({
min : function(a,b){
return a<b?a:b;
},
max : function(a,b){
return a<b?b:a;
}
})
$(function(){
$("input").click(function(){
var a = prompt("请输入一个数:");
var b = prompt("再输入一个数:");
var c = jQuery.min(a,b);
var d = jQuery.max(a,b);
alert("最大值是:" + d + "\n最小值是:" + c);
});
})
<input type="button" value="jQuery扩展测试" />
var a = {name : "aaa",pass : 777};
var b = {name : "bbb",pass : 888,age : 9};
var c = jQuery.extend(a,b);
$(function(){
for(var name in c){
$("div").html($("div").html() + "<br />"+ name + ":" + c[name]);
}
})
jQuery.smalluv = {
min : function(a,b){
return a<b?a:b;
},
max : function(a,b){
return a<b?b:a;
}
}
$(function(){
$("input").click(function(){
var a = prompt("请输入一个数:");
var b = prompt("再输入一个数:");
var c = jQuery.smalluv.min(a,b);
var d = jQuery.smalluv.max(a,b);
alert("最大值是:" + d + "\n最小值是:" + c);
});
})
jQuery.fn.test = function(){
alert("jQuery对象方法");
}
$(function(){
$("div").click(function(){
$(this).test();
});
})
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>最简单的jquery插件</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../res/jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
(function($) {
jQuery.extend({//写法1
a: function(h){
$("#ad").html(h);
},
b:function(h){
alert(h);
}
})
})(jQuery);
(function($) {//写法2
jQuery.a=function(h){
$("#ad").html(h);
}
jQuery.b=function(h){
alert(h);
}
})(jQuery);
$(document).ready(function(){
$.a("abc");
$.b("xyz");
});
</script>
</head>
<body>
<h3>最简单的jQuery插件</h3>
<div id="ad"></div>
</body>
</html>
(function($, window){
// 初始态定义
var _oDialogCollections = {};
// 插件定义
$.fn.MNDialog = function (_aoConfig) {
// 默认参数,可被重写
var defaults = {
// string
sId : "",
// num
nWidth : 400,
// bollean
bDisplayHeader : true,
// object
oContentHtml : "",
// function
fCloseCallback : null
};
var _oSelf = this,
$this = $(this);
// 插件配置
this.oConfig = $.extend(defaults, _aoConfig);
// 初始化函数
var _init = function () {
if (_oDialogCollections) {
// 对于已初始化的处理
// 如果此时已经存在弹框,则remove掉再添加新的弹框
}
// 初始化弹出框数据
_initData();
// 事件绑定
_loadEvent();
// 加载内容
_loadContent();
}
// 私有函数
var _initData = function () {};
var _loadEvent = function () {};
var _loadContent = function () {
// 内容(分字符和函数两种,字符为静态模板,函数为异步请求后组装的模板,会延迟,所以特殊处理)
if($.isFunction(_oSelf.oConfig.oContentHtml)) {
_oSelf.oConfig.oContentHtml.call(_oSelf, function(oCallbackHtml) {
// 便于传带参函数进来并且执行
_oSelf.html(oCallbackHtml);
// 有回调函数则执行
_oSelf.oConfig.fLoadedCallback && _oSelf.oConfig.fLoadedCallback.call(_oSelf, _oSelf._oContainer$);
});
} else if ($.type(_oSelf.oConfig.oContentHtml) === "string") {
_oSelf.html(_oSelf.oConfig.oContentHtml);
_oSelf.oConfig.fLoadedCallback && _oSelf.oConfig.fLoadedCallback.call(_oSelf, _oSelf._oContainer$);
} else {
console.log("弹出框的内容格式不对,应为function或者string。");
}
};
// 内部使用参数
var _oEventAlias = {
click : 'D_ck',
dblclick : 'D_dbl'
};
// 提供外部函数
this.close = function () {
_close();
}
// 启动插件
_init();
// 链式调用
return this;
};
// 插件结束
})(jQuery, window);
var MNDialog = $("#header").MNDialog({
sId : "#footer", //覆盖默认值
fCloseCallback : dialog,//回调函数
oContentHtml : function(_aoCallback){
_aoCallback(_oEditGrpDlgView.el);
}
}
});
// 调用提供的函数
MNDialog.close;
function dialog(){
}
(function($, window) {
// jquery code
})(jQuery, window);
(function(a,b){})(jQuery, window); // jQuery被优化为a, window 被优化为 b
var _oSelf = this, $this = $(this);
this.oConfig = $.extend(defaults, _aoConfig);
(function($){
$.fn.addrInput = function(_aoOptions){
var _oSelf = this;
_oSelf.sVersion = 'version-1.0.0';
_oSelf.oConfig = {
nInputLimitNum : 9
};
// 插件配置
$.extend(_oSelf.oConfig, _aoOptions);
// 调用这个对象的方法,传递this
$.fn.addrInput._initUI.call(_oSelf, event);
$.fn.addrInput._initEvents.call(_oSelf);
// 提供外部函数
this.close = function () {
_close();
}
//返回jQuery对象,便于Jquery的链式操作
return _oSelf;
}
$.fn.addrInput._initUI = function(event){
var _oSelf = this,
_oTarget = $(event.currentTarget);
}
$.fn.addrInput._initEvents = function(){}
})(window.jQuery);
<div id="wrapper"> <a href="#" id="inner">click here!</a> </div>
$('#wrapper').click(function(e) {
console.log('#wrapper');
console.log(e.currentTarget);
console.log(e.target);
});
$('#inner').click(function(e) {
console.log('#inner');
console.log(e.currentTarget);
console.log(e.target);
});
#inner <a href="#" id="inner">click here!</a> <a href="#" id="inner">click here!</a> #wrapper <div id="wrapper"><a href="#" id="inner">click here!</a></div> <a href="#" id="inner">click here!</a>
var MemberCard = function(_aoOption){
// 配置(默认是从外面传进来)
_aoOption || (_aoOption = {}) ;
// 初始化函数
_init(this);
}
var _init = function(_aoSelf) {
// 函数执行
_initData(_aoSelf);
// 调用对象的私有方法
_aoSelf._timedHide();
}
var _initData = function ( _aoSelf ) {}
// 私有方法
MemberCard.prototype._timedHide = function(_aoOptions) {
var _oSelf = this;
clearTimeout(this.iHideTimer);
// 使用underscore.js的extend方法来实现属性覆盖
var oDefault = extend( { nHideTime: 300 }, _aoOptions );
_oSelf.iHideTimer = setTimeout( function(){
// 调用对象的共有方法
_oSelf.hide();
}, oDefault.nHideTime);
}
// 公有方法
MemberCard.prototype.hide = function(_aoOptions) {}
var oColleagueCard = new MemberCard({ nHideTime: 200 });
oColleagueCard.hide();
function getType(o){
return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
}
function extend(destination,source){
for(var p in source){
if(getType(source[p])=="array"||getType(source[p])=="object"){
destination[p]=getType(source[p])=="array"?[]:{};
arguments.callee(destination[p],source[p]);
}else{
destination[p]=source[p];
}
}
}
var test={a:"ss",b:[1,2,3],c:{d:"css",e:"cdd"}};
var test1={};
extend(test1,test);
test1.b[0]="change"; //改变test1的b属性对象的第0个数组元素
alert(test.b[0]); //不影响test,返回1
alert(test1.b[0]); //返回change
jQuery.extend([deep], target, object1, [objectN]);
var options = {id: "nav", class: "header"}
var config = $.extend({id: "navi"}, options); //config={id: "nav", class: "header"}
function EditorUtil() {
this._editorContent = $( '#editor_content' );
this._picBtn = $( '#project_pic' );
this.ieBookmark = null;
}
EditorUtil.prototype = {
consturctor: EditorUtil,
noteBookmark: function() {
},
htmlReplace: function( text ) {
if( typeof text === 'string' ) {
return text.replace( /[<>"&]/g, function( match, pos, originalText ) {
switch( match ) {
case '<':
return '<';
case '>':
return '>';
case '&':
return '&';
case '"':
return '"';
}
});
}
return '';
},
init: function() {
this._memBtn.bind( 'click', function( event ) {
$(".error_content").hide();
return false;
});
}
};
// 初始化富文本编辑器
var editor = new EditorUtil();
editor.init();
MemberCard.prototype._timedHide MemberCard.prototype.hide
EditorUtil.prototype = {
consturctor: EditorUtil,
noteBookmark: function(){},
htmlReplace: function(){}
}
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true
当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖),constructor属性的行为就有点奇怪了,如下示例:
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
function Person(name) {
this.name = name;
};
Person.prototype = {
getName: function() {
return this.name;
}
};
var p = new Person("ZhangSan");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true
function Person(name) {
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
function Person(name) {
this.name = name;
};
Person.prototype = new Object({
getName: function() {
return this.name;
}
});
Person.prototype.constructor = Person;
var p = new Person("ZhangSan");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有